views:

84

answers:

1

I'm reflecting a C++/CLI method that has the following signature:

void foo(long n);

This translates into C# as:

void foo(int modopt(IsLong) n);

How can I find if an int parameter is actually a C++/CLI long by reflection?

+1  A: 

Check the ParameterInfo instance's optional custom modifiers:

bool IsLongParameter(System.Reflection.ParameterInfo p) {
   Type[] modifiers = p.GetOptionalCustomModifiers();
   return modifiers.Contains(typeof(System.Runtime.CompilerServices.IsLong));
}
Mehrdad Afshari
It works, although, for some strange reason, it does not should the method signature happens to be void foo([Out] long n); :(
Anzurio
@AZ: Does the C++/CLI compiler attach a `IsLong` attribute to it in that case at all, or it simply decays to `int`?
Mehrdad Afshari
Using Red Gate's .NET Reflector, it still has the modopt(IsLong) modifier. The complete signature in C# appears : void foo(out int modopt(IsLong) m);
Anzurio
Mehrdad Afshari
I think the Reflection API is lacking that feature: https://connect.microsoft.com/VisualStudio/feedback/details/503412/custom-type-modifiers-modreq-modopt-are-generally-inaccessible-through-the-current-reflection-api
Anzurio
@AZ: I'd count that as a bug, not lack of a feature, since it certainly does retrieve the `modopt` from `long n`. It could easily retrieve it from `[Out] long% n` too.
Mehrdad Afshari