简体   繁体   English

是否可以知道System.Object是否实际指定为动态(C#)?

[英]Is it possible to know if a System.Object was actually specified as dynamic (C#)?

I have a couple of scenarios that cover this, but I'll write specifically about the one that's easier to demonstrate 我有几个方案可以解决这个问题,但我会特别写一个更易于演示的方案

I have a factory interface: 我有一个工厂界面:

interface IFactory
{
  Create<T>();
}

And a piece of code that uses it thus: 以及使用它的一段代码:

public static void func(IFactory f)
{
  var o = f.Create<dynamic>();
}

Now, in an implementation of Create<T>() - is is there any way, via reflection over T , to determine if the caller is intending dynamic dispatch on their object as opposed to a straightforward object ? 现在,在Create<T>()的实现中,是否有任何方法,通过T上的反射来确定调用者是否打算在其对象上进行动态调度而不是直接object I have a class where being able to make that distinction would be quite useful... 我有一个课程,能够做出这种区分是非常有用的......

I've had a look to see if I can find attributes on the type or something like that, but no joy. 我已经看过,看看我是否可以在类型或类似的东西上找到属性,但没有快乐。

dynamic is in the eye of the caller only , so no; dynamic是在调用者的眼睛 ,所以没有; that isn't possible AFAIK. 这是不可能的AFAIK。 As far as Create is concerned it is just object . Create而言,它只是object

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

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