绑定泛型方法委托时出错-签名或安全透明度

本文关键字:安全 透明度 出错 泛型方法 绑定 | 更新日期: 2023-09-27 18:06:17

我正在使用反射来做一些数据库查询,并在创建委托以加快反射的使用一点时,我偶然发现了以下错误:

无法绑定到目标方法,因为它的签名或安全性透明度与委托类型的透明度不兼容。

我有两个方法来创建这些委托,它们都具有相似的结构,但是一个可以工作,另一个不能。它们之间唯一的主要区别是,不起作用的那个有更多的参数,并返回一个泛型类型的List,而工作的那个只接受一个参数,返回一个声明类型的单个值,而不是泛型t。

下面是一些示例代码:

public List<T> GetConnections<T>(IElement element, bool getChildren, bool getParents) where T : IConnectionTable, new()
{
    // do some database stuff and return a List<T> where the constraints on
    // T follow the method description above.
}

创建委托

为清晰而简化

private Func<IElement, bool, bool, List<IConnectionTable>> GetConnectionsDelegate(string connectionType)
{
    // Get the type element from the passed string.
    Type elementType = Type.GetType(connectionType, true);
    // Create the generic method using that type.
    MethodInfo method = typeof(MyClass).GetMethod("GetConnections", new Type[]{ typeof(IElement), typeof(bool), typeof(bool) });
    MethodInfo generic = method.MakeGenericMethod(elementType);
    // Create a delegate of the method to speed up subsequent queries.
    var converted = (Func<IElement, bool, bool, List<IConnectionTable>>)Delegate.CreateDelegate(typeof(Func<IElement, bool, bool, List<IConnectionTable>>), this, generic);
    // the above line is where it dies
}

实际代码将委托保存到私有静态字典中,因此我只需要使用反射一次。

如果我打印出方法泛型的内容,似乎都正确转换了。

Result StandardOutput:  
System.Collections.Generic.List`1[T] GetConnections[T](MyProject.Database.IElement, Boolean, Boolean)
System.Collections.Generic.List`1[MyTestProject.TestConnection] GetConnections[TestConnection](MyProject.Database.IElement, Boolean, Boolean)

我这里的假设是,问题在于泛型列表与IConnectionTable列表返回类型之间的差异,但使该方法返回非泛型列表导致泛型方法中的许多强制转换错误,并且无论如何都是错误的。此外,该代码在测试时运行良好。

它不应该是私有和公共方法之间的差异,因为我的其他委托创建方法是相同的,工作得很好(我也试过改变GetConnectionsDelegate到公共,它没有区别。)

任何帮助都将非常感激。

ndh

绑定泛型方法委托时出错-签名或安全透明度

遇到困难的原因实际上与泛型调用无关,而是与返回类型有关。泛型类不支持协方差,这本质上是您试图通过使用实现IConnectionTable的具体类型返回List<IConnectionTable>来完成的。

一个解决这个问题的方法是使用协变接口集合,比如IEnumerable<T>。此外,您需要通过第二个参数添加一个实际实例,因为this可能指向不正确的上下文。
var converted = (Func<IElement, bool, bool, IEnumerable<IConnectionTable>>)
    Delegate.CreateDelegate(typeo‌​f(Func<IElement, bool, bool, IEnumerable<IConnectionTable>>), new MyClass(), generic);

此外,您可能想要考虑编译表达式而不是委托,因为它们可以很好地处理您的情况,并为您提供更多的可读性和潜在的更大的灵活性。您将需要对每种方法进行分析,并确定哪种方法的性能更好。

下面是一个简单的缓存编译表达式示例,它正确地从编译表达式中输出"we got called"。

void Main()
{
    var key = typeof(ConnectionTypeOne).FullName;
    Func<IElement, bool, bool, IEnumerable<IConnectionTable>> expr = 
        _cache.ContainsKey(key) ? _cache[key]
        : CreateConnectionExpression<ConnectionTypeOne>(key);
    expr(new Element(), true, true);
}
private static IDictionary<string, Func<IElement, bool, bool, IEnumerable<IConnectionTable>>> _cache = 
    new Dictionary<string, Func<IElement, bool, bool, IEnumerable<IConnectionTable>>>();
private Func<IElement, bool, bool, IEnumerable<IConnectionTable>> CreateConnectionExpression<T>(string connectionType)
    where T : IConnectionTable
{
    // Get the type element from the passed string.
    Type elementType = Type.GetType(connectionType, true);
    // Create the generic method using that type.
    MethodInfo method = typeof(MyClass).GetMethod("GetConnections", new Type[] { typeof(IElement), typeof(bool), typeof(bool) });
    MethodInfo generic = method.MakeGenericMethod(elementType);
    var instance = Expression.Constant(new MyClass());
    var c1 = Expression.Parameter(typeof(IElement));
    var c2 = Expression.Parameter(typeof(bool));
    var c3 = Expression.Parameter(typeof(bool));
    var expr = Expression.Call(instance, generic, c1, c2, c3);
    Func<IElement, bool, bool, IEnumerable<IConnectionTable>> compiledExpr =
        (Func<IElement, bool, bool, IEnumerable<IConnectionTable>>)
            Expression.Lambda(expr, c1, c2, c3).Compile();
    _cache[connectionType] = compiledExpr;
    return compiledExpr;
}
public class MyClass 
{
    public List<T> GetConnections<T>(IElement element, bool getChildren, bool getParents) 
        where T : IConnectionTable, new()
    {
        Console.WriteLine("we got called");
        return new List<T>();
    }
}
public interface IElement { }
public interface IConnectionTable { }
public class Element : IElement { }
public class ConnectionTypeOne : IConnectionTable { }