系统.反射.类型包含获取接口映射 http://msdn.microsoft.com/en-us/library/system.type.getinterfacemap.aspx这有助于确定哪些方法从接口实现某些方法。
Does 莫诺·塞西尔包含这样的东西?
或者如何实施这种行为?
不,Cecil 不提供这样的方法,因为 Cecil 只给我们原样的 CIL 元数据。 (有一个项目 Cecil.Rocks,其中包含一些有用的扩展方法,但不是这个)
在 MSIL 中,方法有一个属性“overrides”,它包含对此方法重写的方法的引用(在 Cecil 中,MethodDefinition 类中确实有一个属性 Overrides)。但是,该属性仅在某些特殊情况下使用,例如显式接口实现。通常该属性留空,并且哪些方法被相关方法覆盖是基于约定的。这些约定在 ECMA CIL 标准中进行了描述。简而言之,方法会覆盖具有相同名称和相同签名的方法。
以下代码片段可能会对您以及本次讨论有所帮助:http://groups.google.com/group/mono-cecil/browse_thread/thread/b3c04f25c2b5bb4f/c9577543ae8bc40a http://groups.google.com/group/mono-cecil/browse_thread/thread/b3c04f25c2b5bb4f/c9577543ae8bc40a
public static bool Overrides(this MethodDefinition method, MethodReference overridden)
{
Contract.Requires(method != null);
Contract.Requires(overridden != null);
bool explicitIfaceImplementation = method.Overrides.Any(overrides => overrides.IsEqual(overridden));
if (explicitIfaceImplementation)
{
return true;
}
if (IsImplicitInterfaceImplementation(method, overridden))
{
return true;
}
// new slot method cannot override any base classes' method by convention:
if (method.IsNewSlot)
{
return false;
}
// check base-type overrides using Cecil's helper method GetOriginalBaseMethod()
return method.GetOriginalBaseMethod().IsEqual(overridden);
}
/// <summary>
/// Implicit interface implementations are based only on method's name and signature equivalence.
/// </summary>
private static bool IsImplicitInterfaceImplementation(MethodDefinition method, MethodReference overridden)
{
// check that the 'overridden' method is iface method and the iface is implemented by method.DeclaringType
if (overridden.DeclaringType.SafeResolve().IsInterface == false ||
method.DeclaringType.Interfaces.None(i => i.IsEqual(overridden.DeclaringType)))
{
return false;
}
// check whether the type contains some other explicit implementation of the method
if (method.DeclaringType.Methods.SelectMany(m => m.Overrides).Any(m => m.IsEqual(overridden)))
{
// explicit implementation -> no implicit implementation possible
return false;
}
// now it is enough to just match the signatures and names:
return method.Name == overridden.Name && method.SignatureMatches(overridden);
}
static bool IsEqual(this MethodReference method1, MethodReference method2)
{
return method1.Name == method2.Name && method1.DeclaringType.IsEqual(method2.DeclaringType);
}
// IsEqual for TypeReference is similar...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)