确定类型兼容性 [英] Determining type compatiblity

查看:91
本文介绍了确定类型兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们!我已将自己编码到另一个角落……再次!我需要一个专家.

这一次,我正在编写代码,该代码检查对方法调用的参数列表是否有效.为什么?因为我使用从XML文件读取的参数来动态调用各种方法.为什么?不要问... looooong的故事.

很显然,对我的例程的调用可能看起来像

Hi folks! I''ve coded myself into another corner... again! I need a guru.

This time, I''m writing code that checks that the parameter list for a call to a method is valid. Why? Because I am dynamically calling various methods with parameters read from an XML file. Why? Don''t ask... looooong story.

Breifly, a call to my routine might look like

StandardTest.PerformDALvsDBTest(<br />  "DALRoutines",    // the class that will contain the method to call<br />  "InsertCustomer", // the method to be called<br />  new object[] { <br />    DateTime.Parse(parm0), <br />    parm1, <br />    Int32.Parse(parm2)<br />  }<br />);



好.因此,我的例程经过了一堆检查,最后获得了对要调用的方法的引用(MethodInfo.methodRef).该方法的调用将使用



Okay. So my routine goes through a bunch of checks and finally obtains a reference to the method to be called (MethodInfo.methodRef). The call to the method will be made using

methodRef.Invoke(null, myBindings, null, Params, null);


进行,但是首先,我必须确保object[] Params中的所有项目均为正确的类型.为此,我遍历methodRef.GetParameters()并针对每个Params[index].GetType()检查每个.
如果参数数量正确,并且它们都是正确的类型,等等,那么我将执行Invoke() ,否则,我抛出一个错误,指示方法名称,参数,预期类型和使用的类型:
例如"方法[DALRoutines.InsertCustomer]的参数列表中参数#0的System.Type为System.DateTime;应为System.TimeSpan ".我的问题:
我有一个期望参数为Int32的方法.但是,当在object[] Params中传递值时,它似乎已更改为Int16(即使我如上所述专门指出它是Int32,我也认为这是因为它是一个很小的值).当我的例程开始比较参数类型时,它会发出一条错误消息,指出在期望Int32时将其传递给Int16.更重要的是,如果对Invoke()进行了调用,它仍然可以正常工作-.NET似乎并不介意,并且固有地处理了转换.但是我找不到一种方法来说明它会起作用-我只能看到两种类型是不同的.

我尝试研究不同的方法来判断类型是否兼容(包括Type.IsSubclassOf()Type.IsAssignableFrom()),但我似乎找不到任何让我知道Int16可用作Int32参数值的东西.

有人知道吗做到这一点的方法?


But first, I must make sure that all the items in the object[] Params are of the right type. To do that I loop through methodRef.GetParameters() and check each one against each Params[index].GetType().
If there are the correct number of parameters, and they are all of the correct type, etc, then I will perform the Invoke(), otherwise, I throw an error indicating the method name, the parameter, the expected type and the type that was used:
e.g. "The System.Type for parameter #0 in the parameter list for the method [DALRoutines.InsertCustomer] is System.DateTime; System.TimeSpan was expected".

So, here is my problem:
I have a method that expects a parameter to be an Int32. However, when the value is passed in the object[] Params, it seems to be changed to Int16 (even if I specifically indicate that it is Int32 as above - I assume that it is because it is a small value). When my routine starts comparing parameter types it spits out an error saying it was passed an Int16 when it expects an Int32. The kicker is that the call to Invoke() would still work if it was made - .NET does not seem to mind and handles the conversion inherently. But I can''t find a way to tell that it would work - all I can see is that the two types are different.

I have tried investigating different ways to tell if the types are compatible (including Type.IsSubclassOf() and Type.IsAssignableFrom()) but I can''t seem to find anything that will let me know that an Int16 can be used as the value for an Int32 parameter.

Does anyone know a way to do this?

推荐答案

这样可以吗?

Would something like this be okay?

<br />		public static bool ImplicitCastAllowed(Type type, object value)<br />		{<br />			try<br />			{<br />				Convert.ChangeType(value, type);<br />				return true;<br />			}<br />			catch<br />			{<br />				return false;<br />			}<br />		}<br />



问候



regards


这篇关于确定类型兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆