开发者

C#/vb.net type mismatch looking up a constructor by reflection (Integer() vs System.Int32[])

开发者 https://www.devze.com 2022-12-18 07:48 出处:网络
I\'m passing a type name and some parameters from C# code into a navigation framework written in VB.The navigation framework looks for a constructor on 开发者_运维百科the type that matches the paramet

I'm passing a type name and some parameters from C# code into a navigation framework written in VB. The navigation framework looks for a constructor on 开发者_运维百科the type that matches the parameters passed in using Type.GetConstructor(Types()). The constructor that I'm looking for expects an array of integers -- Integer() in vb. But it gets an array of System.Int32. I've gone so far as to try this:

           System.Int32[] int32Array = IdList.ToArray();
            int[] intArray = new int[int32Array.Length];
            for (int i = 0; i < int32Array.Length; i++ )
            {
                intArray[i] = (int)int32Array[i];
            }

And the VB code still sees System.Int32 on the other end, which means that it doesn't find the constructor.

Any insights?


As C# int is a syntatic sugar for System.Int32, VB Integer is also a syntatic sugar for the same type. So it shouldn't be any problem calling one or another.

However, I'd check the parameter types of the constructor info returned by the GetConstructor method.


I'm going to take a guess in that you've done the same mistake I've done a few times.

When you call Type.GetConstructor(Type[]), and building an array of types, I've sometimes produce an array of type objects, one for each element of the array.

Let me explain.

I have an array of integers (System.Int32), and want to find the constructor that takes a single parameter that is an array of such integers.

Now, the right way, in C#, to produce a type array that can be passed to GetConstructor is like this:

Type[] types = new Type[] { typeof(Int32[]) };

instead, I've on a few occasions written code like this:

Type[] types = (from v in arr select v.GetType()).ToArray();

It's a boneheaded mistake, but this change makes GetConstructor look for a constructor with the same number of parameters as there are values in my array.

Perhaps you've done the same thing?

Since you're not actually showing us the code that calls reflection, it's just speculation however.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号