简体   繁体   English

使用反射传递参数时,Visual Studio强制转换是多余的

[英]Visual studio cast is redundant when passing arguments using reflection

With this line of code, VS2015 shows "Cast is redundant" for both arguments (but works!!). 使用这行代码,VS2015显示两个参数的“Cast is redundant”(但有效!!)。

Activator.CreateInstance(type, (IOperationContext)null, (Dictionary<string, object>)null);

Whereas if I remove them I get: 如果我删除它们,我得到:

Activator.CreateInstance(type, null, null);

It throws " System.MissingMethodException " exception: 它抛出“ System.MissingMethodException ”异常:

Constructor on type 'iBI.Services.Hsd.Operations.Schedule.RefreshUsedOperation' not found. 未找到类型'iBI.Services.Hsd.Operations.Schedule.RefreshUsedOperation'的构造函数。

Is there something wrong with the usage or this is just false alram by visual studio? 使用有什么问题,或者这只是视觉工作室的假alram?

When you call 你打电话时

Activator.CreateInstance(type, null, null)

it actually calls 它真的叫

CreateInstance(Type type, object[] args, object[] activationAttributes)

method overload and this CreateInstance overload looks for parameterless ctor which cannot be found. 方法重载和此CreateInstance重载查找无法找到的无参数ctor。

When you call 你打电话时

Activator.CreateInstance(type, (IOperationContext)null, (Dictionary<string, object>)null);

then it calls 然后它打电话

CreateInstance(Type type, params object[] args)

overload. 超载。 And then ctor with two params found and called. 然后发现并召唤了两个参数。 Both params passed as null. 两个参数都传递为null。

I would say it's a false alarm by VS, since reflection needs those casts to know which construction to call. 我会说这是VS的误报,因为反射需要那些演员知道要调用哪个结构。 Without those casts, the parameters you give have no types, which makes it impossible for the Activator to know which signature to use, especially if you have multiple constructors which take 2 parameters of different types. 如果没有这些转换,您提供的参数没有类型,这使得Activator无法知道要使用哪个签名,特别是如果您有多个构造函数,这些构造函数采用不同类型的2个参数。 The warning probably comes from the fact that this would indeed be redundant in almost all other situation, since a null can fit in all reference types (and the compiler will throw you an error if you try to put a null in a value type) and will (kinda) take the type of its container. 警告可能来自这样的事实:在几乎所有其他情况下这确实是多余的,因为null可以适用于所有引用类型(如果您尝试在值类型中放置null,编译器将抛出错误)和将(有点)采取其容器的类型。 At this point, if you wish you could open a bug report with Visual Studio as this is indeed something that could (should?) be fixed. 此时,如果您希望可以使用Visual Studio打开错误报告,因为这确实可以(应该?)修复。

Actually, Visual Studio is kind of correct in that having both casts is redundant. 实际上,Visual Studio是正确的,因为两个转换都是多余的。 If you remove either one of the casts but leave the other one there, VS will stop complaining because you don't have any redundant casts, but will still have enough information in place to invoke the correct method overload: 如果您删除其中一个强制转换但将另一个强制转移,VS将停止抱怨,因为您没有任何冗余强制转换,但仍会有足够的信息来调用正确的方法重载:

Both of these options are fine: 这两个选项都很好:

Activator.CreateInstance(type, null, (Dictionary<string, object>)null);
Activator.CreateInstance(type, (OperationContext)null, null);

It's not problem with your IDE. 这与您的IDE没有问题。 Just open MSDN and look to overrides: this and this . 只需打开MSDN并查看覆盖: When you wrote: 你写的时候:

Activator.CreateInstance(type, (IOperationContext)null, (Dictionary<string, object>)null);

.NET know, that it should use the second method . .NET知道,它应该使用第二种方法 When your wrote: 当你写道:

Activator.CreateInstance(type, null, null);

I think, .NET interprete it as first method , that's why you have problem. 我认为,.NET将其解释为第一种方法 ,这就是你遇到问题的原因。 You can try to write next code: 您可以尝试编写下一个代码:

Activator.CreateInstance(youType, args:null, activationAttributes:null);

And it should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM