c# - 如何知道 MemberInfo 是否是属性的显式实现

标签 c# .net reflection

假设我有以下代码。如何通过反射获取 MemberInfo/PropertyInfo 以“显式”实现 Test.Name

此外,有没有什么方法可以通过编程知道 MemberInfo 是接口(interface)属性的显式实现?

public interface ITest
{
    string Title { get; set; }
}

public interface IExplicit
{
    string Name { get; set; }
}

public class Test : ITest,IExplicit
{

    public string Title { get; set; }

    string IExplict.Name
    {
        get
        {
            return this.Title;

        }
        set
        {

        }
    }
}

最佳答案

假设您有这个界面:

interface ITest
{
    bool MyProperty { get; set; }
}

在这个类中实现:

class Test : ITest
{
    bool ITest.MyProperty { get; set; }
}

现在让我们将此属性添加到 Test(注意它们具有相同名称):

public bool MyProperty { get; set; }

使用 plain GetProperties(),您不会获得显式接口(interface)实现(因为它始终是私有(private)成员):

int count = new Test().GetType().GetProperties().Length; // It's 1!

如果您同时包含 PublicNonPublic 成员,您将获得两者。要区分它们,您可以首先依靠名称:显式实现将包含完整的接口(interface)名称(因此您可以查找 .普通 属性,因为它不是允许的字符):

public static bool IsExplicitInterfaceImplementation(PropertyInfo property)
{
    return property.Name.Contains(".");
}

这有点天真,所以您可能需要一些额外的检查,您可以断言该属性的 get 方法将:

  • 虚拟密封
  • 私有(private)的
  • 至少包含一个点。
  • 不会以 get__set 开头

让我们更改代码:

public static bool IsExplicitInterfaceImplementation(PropertyInfo property)
{
    // This check is not mandatory and not cross-languages.
    // How this method is named may vary
    if (!property.Name.Contains("."))
        return false;

    if (property.Name.StartsWith("get_"))
        return false;

    if (!property.GetMethod.IsFinal)
        return false;

    if (!property.GetMethod.IsVirtual)
        return false;

    if (!property.GetMethod.IsPrivate)
        return false;

    return true;
}

当然不需要所有这些检查,我认为前两个足以排除大部分编译器生成的代码。

如果您知道可以显式实现哪个接口(interface),您会发现这里的这个问题非常有用:How to find if a method is implementing specific interface

编辑
从评论中我想到了这一点,我发现没有合适的方法来做到这一点,CLR 不应用任何规则(AFAIK),因为需要的只是接口(interface)方法和类方法之间的链接(无论它如何调用)。我想(但它可能会放宽或扩展到其他语言,如果有人愿意为更多测试做出贡献,我会将这个答案作为 wiki)这段代码在大多数情况下可能有效(感谢 Alxandr 的提示):

检查方法(给定 MethodInfo)是否为显式接口(interface)实现的第一个通用函数。

我们不能断言的是:

  • 我们不能使用名称(例如检查“.”),因为它依赖于实现(C# 使用 interfaceName.methodName 但其他语言不使用)。

  • 我们不能依赖检查私有(private),因为(例如)在 C++/CLI 中,它可以是一个公共(public)方法(使用另一个名称),而且一个接口(interface)可以被“黑化”为内部的,但实现者可以公开(因此方法也不会公开)。

我们可以断言:

  • 显式接口(interface)实现始终是密封的和虚拟的。可能并非所有语言都适用,因此我们可能会放宽此规则。

  • 如果一个方法与它实现的接口(interface)中声明的方法名称不同,那么它就是一个显式实现。

这是代码:

public static bool IsExplicitInterfaceImplementation(MethodInfo method)
{
    // Check all interfaces implemented in the type that declares
    // the method we want to check, with this we'll exclude all methods
    // that don't implement an interface method
    var declaringType = method.DeclaringType;
    foreach (var implementedInterface in declaringType.GetInterfaces())
    {
        var mapping = declaringType.GetInterfaceMap(implementedInterface);

        // If interface isn't implemented in the type that owns
        // this method then we can ignore it (for sure it's not
        // an explicit implementation)
        if (mapping.TargetType != declaringType)
            continue;

        // Is this method the implementation of this interface?
        int methodIndex = Array.IndexOf(mapping.TargetMethods, method);
        if (methodIndex == -1)
            continue;

        // Is it true for any language? Can we just skip this check?
        if (!method.IsFinal || !method.IsVirtual)
            return false;

        // It's not required in all languages to implement every method
        // in the interface (if the type is abstract)
        string methodName = "";
        if (mapping.InterfaceMethods[methodIndex] != null)
            methodName = mapping.InterfaceMethods[methodIndex].Name;

        // If names don't match then it's explicit
        if (!method.Name.Equals(methodName, StringComparison.Ordinal))
            return true;
    }

    return false;
}

用这个辅助函数来做属性检查:

public static bool IsExplicitInterfaceImplementation(PropertyInfo property)
{
    // At least one accessor must exists, I arbitrary check first for
    // "get" one. Note that in Managed C++ (not C++ CLI) these methods
    // are logically separated so they may follow different rules (one of them
    // is explicit and the other one is not). It's a pretty corner case
    // so we may just ignore it.
    if (property.GetMethod != null)
        return IsExplicitInterfaceImplementation(property.GetMethod);

    return IsExplicitInterfaceImplementation(property.SetMethod);
}

关于c# - 如何知道 MemberInfo 是否是属性的显式实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17853671/

相关文章:

c# - 错误提示:找不到方法:“无效RestSharp.RestClient.set_BaseUrl(System.String)”。在特维里奥沙普

c# - 如何只显示数据表中的某些列?

scala - 使用反射和解释器动态解析字符串并在 Scala 中返回一个函数

c# - 这是在编辑模式下使用 GridView 格式的错误还是复杂的编码解决方案?

C#获取RDC/RDP和 "Console"Session信息

c# - 花在同步上的线程时间是否太高?

c# - 降低字符串时解析枚举

c# - 引用类型——我们能看到实际的引用吗?

c# - 如何摆脱这段代码中的反射调用?

java - 了解 Java 反射的缺点