C# 动态委托转换为动态函数

C# 动态委托转换为动态函数,c#,delegates,rhino-mocks,C#,Delegates,Rhino Mocks,我想将一个对象复制到同一接口的模拟中。目标是,动态地完成它。但似乎没有办法将动态委托转换为函数 public static T GetCopiedMock<T>(T toCopy, T mockObject) where T : class { IEnumerable<PropertyInfo> properties = GetPropertyInfos(toCopy, typeof(T)); foreach (var property in prope

我想将一个对象复制到同一接口的模拟中。目标是,动态地完成它。但似乎没有办法将动态委托转换为函数

public static T GetCopiedMock<T>(T toCopy, T mockObject) where T : class
{
    IEnumerable<PropertyInfo> properties = GetPropertyInfos(toCopy, typeof(T));

    foreach (var property in properties)
    {
        var parameter = Expression.Parameter(typeof(T));
        var result = Expression.Property(parameter, property);
        var lamda = Expression.Lambda(result, parameter);
        var compilat = lamda.Compile();
        var funcType = typeof(Func<,>).MakeGenericType(typeof(T), property.PropertyType);

        //Here is my problem
        mockObject.Expect(new Func<T, property.PropertyType>(compilat));
    }

    return mockObject;
}
我知道这是不可能的,因为propertytype是在运行时的,但是有什么解决方法吗

顺便说一下,这是我的第一篇文章。所以,如果你看到我必须做得更好的可怕事情,告诉我

未使用funcType变量。你想用它做什么?还有,我找不到你正在使用的Expect方法?这是框架的一部分吗?由于T是一个类,默认情况下,它将只与System.Object方法关联。现在没有funcType。我认为它有帮助,也许Expect是Rhino Mock的一个扩展。此方法需要类似于Func的参数