实际答案在答案的评论中.我期待得到我的界面的"实例",你不能这样做.
-
我找到了一种方法来做我真正想要的,对于任何有兴趣的人,我已在下面概述.
public interface Interface<T> { Func<T,T> Property { get; set; } }
public class Concrete : Interface<string>
{
public Concrete()
{
(this as Interface<string>).Property = (s) => { return $"hello, {s}!"; };
}
Func<string, string> Interface<string>.Property
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
object obj = new Concrete();
var propInfo = obj.GetType().GetInterfaces().Single().GetProperty("Property");
dynamic func = propInfo.GetMethod.Invoke(obj, null);
var output = func("world");
}
}
Run Code Online (Sandbox Code Playgroud)
-
我正在做一些codegen并且正在大量使用动态类型,不幸的是我遇到了动态类型/显式接口难题.
我可以通过使用这里Convert.ChangeType(...)概述的方法解决这个问题,但是它要求实现这一点,这将产生很大的开销,我不想这么做.IConvertable