tags:

views:

544

answers:

3

In C#, I am using a library that defines an enum. I would like to allow consumers of my code (in a different assembly) to pass in an enum value as a parameter to one of my functions without having to reference the underlying library themselves.

Is there a way for me to expose the library's enumeration to my consumers?

+5  A: 

You could define your own enum with the values you want to support, expose that to your consumers, and simply convert it to the library's enum before you call into it. Since enums are just numbers behind the scenes, it's easy to convert one enum's values to another's.

jezell
Within one minute you got three ups and an answer?
Will
I was watching the question pretty closely, and I suspected this was the way I was going to have to go.So I gave him one of the ups and the answer. I don't think it's particularly mysterious that he got two other up votes.
brien
The question didn't strike me as exciting, it was like 7:30 AM EST. Not accusing anybody of anything, but it was kinda weird to see that much action that fast.
Will
Yeah, I'm always amazed that I can get quality responses to my stupid questions first thing in the morning. stackoverflow is great!
brien
7:30AM EST is 12:30 midday in the UK and there are a lot of UK developers active on this site. Lunchtime seems as good a time as any to be checking this place.
Jeff Yates
+2  A: 

You can offer an overload that takes an int, describe which values are valid, then perform the cast yourself. Alternatively, you can offer a different enumeration in your library, then convert it before calling into the second library.

Will
A: 

I realy don't know, which rational reason is doing this ( combination of "consumer call method from type in my assembly" and "consumer doesn't have my assembly as reference" ), but there are 2 ways.

The first ( recommended ): You should split your assembly into two. One with enum type definition and the second with the functions. Consumers will reference only "first" assembly.

The second ( not recommended ): You can use a (sbyte/(u)short/(u)int/(u)long in parameters instead of enums.

But I think, you have a mismatch design of object model.

TcKs