Language-based conditional code?

I am writing a library for C #. I am wondering if only methods / fields are available if the library is used in a C # project and if it is used in another .NET language such as Visual Basic the methods will not be available. The reason for this is that there are some functions / fields that are only useful for unsafe code and it would be a bit silly if they were available to Visual Basic if they didn't serve the purpose.

Is it possible to only have specific classes / methods / fields depending on the language they are used in? If it weren't easy for me to get two separate assemblies available for download, i.e. For C # and for VB.Net. Or I could just include additional methods independently (but I would like to prevent confusion with those unfamiliar with pointers and unsafe code, just so that users don't mess with unsafe methods and accidentally do something stupid, but I think it really is irrelevant!).

Thank!

+3


source to share


2 answers


Is it possible to only have specific classes / methods / fields depending on the language they are used in?

Not.

I would like to prevent confusion with those unfamiliar with pointers and unsafe code, just so that users don't mess with unsafe methods and accidentally do something stupid.



Use abstraction. Create an assembly with interfaces / base classes and give some consumers an assembly along with another with safe implementations and other consumers a base assembly along with another that provides advanced functionality.

Alternatively, you can use a single assembly with internal methods and use InternalsVisibleToAttribute if you know which "advanced" assemblies will consume it.

+2


source


As far as I know, there is no way to determine the assembly language calling one of yours, since by the time this all happens, everyone is saying IL anyway (or rather compiled instructions, but whatever).

I think it is best in this case to build two different versions of the assembly based on conditional expressions and provide them to your users depending on which language they are using. Or you can just include all the code and tell your VB users to access some of the functionality.



I must admit that I'm not sure what you could do here to expose unsafe ringtones to callers; some IntPtr

or something?

0


source







All Articles