本文介绍了Reflection中的Inconsistant null参数处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




似乎反射解析方法的方式与

默认CLR不完全相同。例如这个简单的类:


class Test {

public void Hello(string name){

Console.WriteLine(" ; Hello(string)");

}

public void Hello(object obj){

Console.WriteLine(" Hello(对象)");

}

}


如果我通常使用null参数调用该方法:


测试测试=新测试();

test.Hello(null); //显示Hello(字符串)


但是如果我只知道输入参数(为空)和

方法的名称,并试图使用反射来解析要调用的方法,它会变成:b
$ b测试测试=新测试();

MethodInfo方法= test.GetType()。GetMethod(" Hello",new Type [] {

typeof(void)});

method.invoke(test,new object [] { 空值 }); //显示Hello(对象)


结果发生了变化。它必须是因为typeof(void)的东西,但是我不能说b $ b说typeof(string)因为输入参数为null。我该怎么做才能获得相同的结果?

谢谢,

Stefan

Hi,

It seems that the way reflection resolves methods is not quite the same as
default CLR. For example this simple class:

class Test {
public void Hello(string name) {
Console.WriteLine("Hello(string)");
}
public void Hello(object obj) {
Console.WriteLine("Hello(object)");
}
}

If I invoke the method normally using null parameter:

Test test = new Test();
test.Hello(null); // shows Hello(string)

But if I only know the input parameter (which is null) and the name of the
method, and trying to use reflection to resolve the method to invoke, it
becomes:

Test test = new Test();
MethodInfo method = test.GetType().GetMethod("Hello", new Type[] {
typeof(void) });
method.invoke(test, new object[] { null }); // shows Hello(object)

The result changed. It must because of the typeof(void) thing, but I can''t
say typeof(string) because the input parameter is null. What should I do to
get the same result?
Thanks,
Stefan

推荐答案




即使输入参数为null,你仍然可以使用typeof(string)。

(我很惊讶你可以在上面输入typeof(无效),或者说是b $ b诚实。)


如果你只收到null你基本上不知道用什么方法来调用b
,但你要调用最具体的方法,你需要

来电类型.GetMethods并找出自己需要的人。


-

Jon Skeet - < sk *** @ pobox.com>


如果回复该组,请不要给我发邮件



You can still use typeof(string) even if the input parameter is null.
(I''m surprised you can get away with typeof(void) in the above, to be
honest.)

If you only receive null and you basically don''t know which method to
call, but you want to call the most specific method, you''ll need to
call Type.GetMethods and work out which one to call for yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





< snip>

你可以即使输入参数为空,仍然使用typeof(字符串)。
(我很惊讶你可以在上面使用typeof(void),以实现。)

如果你只收到null并且你基本上不知道要调用哪种方法,但你想调用最具体的方法,你需要调用Type.GetMethods并找出哪一个可以自己调用。



<snip>

You can still use typeof(string) even if the input parameter is null.
(I''m surprised you can get away with typeof(void) in the above, to be
honest.)

If you only receive null and you basically don''t know which method to
call, but you want to call the most specific method, you''ll need to
call Type.GetMethods and work out which one to call for yourself.




嗯...完全模仿C#编译器使用的类型绑定规则似乎

不容易且容易出错,.net

框架库中是否存在这样的Binder类?

谢谢,

Stefan



Hmm... to completely mimic the type binding rule used by C# compiler seems
not easy and error-prone, is there a such Binder class exists in the .net
framework library already?
Thanks,
Stefan




嗯...完全模仿C#使用的类型绑定规则编译器似乎不容易且容易出错,.net
框架库中是否存在这样的Binder类?



Hmm... to completely mimic the type binding rule used by C# compiler seems
not easy and error-prone, is there a such Binder class exists in the .net
framework library already?




我不喜欢我不相信,我很害怕。我会尽量避免这种情况,个人而言,但是如果没有更多关于你的问题的信息,很难确切知道这会带来什么。


-

Jon Skeet - < sk *** @ pobox.com>


如果回复该群组,请不要给我发邮件



I don''t believe so, I''m afraid. I would try to avoid it, personally,
but it''s hard to know exactly what that entails without having more
information about your problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于Reflection中的Inconsistant null参数处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:09