静态实例和MemberInfo的相等性 [英] Equality of static instance and MemberInfo

查看:91
本文介绍了静态实例和MemberInfo的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

我有一堆静态成员字段,如下所示.在整个应用程序中,FirstEntry应该是唯一的.
现在,我要记录"testEntry"所指向的其中之一.
从System.Object继承的ToString()方法返回类名称.我需要它来返回变量名.所以我覆盖了ToString()并得到了所有有问题的静态字段.

由于MemberInfo是完全不同的类型,因此无法直接将this与每个可能的MemberInfo进行比较.因此,我尝试实例化对象MemberInfo持有有关该对象的信息并与之进行比较.但是我总是得到null参考.

如何将实例(在这种情况下为this)与MemberInfo表示的对象进行比较?

Hi experts,

I have a bunch of static member fields like shown below. Throughout the application, a FirstEntry should be unique.
Now I want to log which one of those is pointed to by "testEntry".
The ToString() method inherited from System.Object returns the class name. I need it to return the variable name instead. So I overrode ToString() and got all the static fields in question.

Comparing this to every possible MemberInfo directly cannot work since MemberInfo is of a totally different type. So I try to instantiate the object MemberInfo holds information about and compare to that. But I always get a null reference.

How can I compare an instance, this in this case, to the object a MemberInfo represents?

public class BaseClass
{
    public static BaseClass FirstEntry = new BaseClass();
    public static BaseClass SecondEntry = new BaseClass();
    public static BaseClass ThirdEntry = new BaseClass();


    public override string ToString()
    {
        System.Reflection.MemberInfo[] members = this.GetType().GetMembers(
            System.Reflection.BindingFlags.FlattenHierarchy
            | System.Reflection.BindingFlags.Static
            | System.Reflection.BindingFlags.Public
        );
        foreach (System.Reflection.MemberInfo member in members)
        {
            // Obviously cannot work
            if (this.Equals(member))
            {
                return (member.Name);
            }

            // Always returns NULL
            object o = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(member.Name, false);
        }
        return (base.ToString());
    }
}


public class DerivedClass : BaseClass
{
    public static DerivedClass FirstDerivedEntry = new DerivedClass();
    public static DerivedClass SecondDerivedEntry = new DerivedClass();
}


class Program
{
    static void Main(string[] args)
    {
        // Some logic determines what entry to use.
        DerivedClass testEntry = DerivedClass.SecondDerivedEntry;

        // Output what entry was determined.
        Console.WriteLine(BaseClass.firstEntry.ToString());
    }
}

推荐答案

要查看您当前正在执行ToString的静态成员的哪个实例,需要您获取值,然后查看是否等于this,这样的事情可能会解决问题;

To see which instance of the static members you''re currently executing ToString for requires you to get the value and then see if that is equal to this, something like this might do the trick;

public override string ToString()
{
  FieldInfo[] fields = GetType().GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);
  foreach (FieldInfo field in fields)
  {
    object value = field.GetValue(this);
    if (Object.ReferenceEquals(value, this))
      return field.Name;
  }
  return base.ToString();
}



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik


这篇关于静态实例和MemberInfo的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆