gpt4 book ai didi

c# - 绑定(bind)泛型方法委托(delegate)时出错 - 签名或安全透明度

转载 作者:太空宇宙 更新时间:2023-11-03 23:10:33 24 4
gpt4 key购买 nike

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

Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

我有两种创建这些委托(delegate)的方法,它们都具有相似的结构,但一种有效,另一种无效。它们之间唯一的主要区别是,不起作用的那个有更多的参数并返回一个具有泛型类型的列表,而工作的那个只接受一个参数并返回一个声明类型的值而不是泛型 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.
}

创建委托(delegate)

为清楚起见进行了简化

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
}

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

如果我打印出 methodgeneric 的内容,它似乎都已正确转换。

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)

我这里的假设是,问题在于泛型 List 与 IConnectionTable List 返回类型之间的区别,但是使方法返回非泛型列表会导致泛型方法中出现很多转换错误,这样做有点不对反正。另外,该代码在测试时运行良好。

这不应该是私有(private)方法和公共(public)方法之间的区别,因为我的其他委托(delegate)创建方法是相同的并且工作正常(我也尝试将 GetConnectionsDelegate 更改为 public,但没有任何区别.)

如有任何帮助,我们将不胜感激。

最佳答案

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

解决方法是使用协变接口(interface)集合,例如 IEnumerable<T> .此外,您需要通过第二个参数添加一个实际实例,因为 this可能指向不正确的上下文。

var converted = (Func<IElement, bool, bool, IEnumerable<IConnectionTable>>)
Delegate.CreateDelegate(typeo‌​f(Func<IElement, bool, bool, IEnumerable<IConnectionTable>>), new MyClass(), generic);

此外,您可能需要考虑编译表达式而不是委托(delegate),因为它们可以很好地适应您的情况,并为您提供更多的可读性和潜在的灵 active 。您需要分析每种方法并确定哪种方法效果更好。

下面是一个简单的缓存编译表达式示例,它从编译表达式中正确输出“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 { }

关于c# - 绑定(bind)泛型方法委托(delegate)时出错 - 签名或安全透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39378899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com