Dynamic parameter causes compiler to think method return is dynamic

前端 未结 1 1848
说谎
说谎 2020-12-09 08:16

If I have a dynamic parameter the compiler seems to ditch the return type and think it\'s dynamic.

For example:

public MethodResult IsValid(object us         


        
1条回答
  •  醉梦人生
    2020-12-09 08:45

    Yes, dynamic stops the compiler from knowing the type on any parameters, properties, or method return types. Add an explicit cast like:

    (MethodResult)IsValid(someObject));
    

    The reason here is that once you enter the dynamic world in C# you are going into late binding. The compiler can't verify this code because it can no longer use any static type analysis. So it defers until later. You can help overcome this by providing static casts as a guide for the compiler.

    0 讨论(0)
提交回复
热议问题