Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
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());
    }
}
Posted

1 solution

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;

C#
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();
}


Hope this helps,
Fredrik
 
Share this answer
 
Comments
lukeer 12-Mar-12 10:07am    
Thanks a lot, it works like a charm.
Fredrik Bornander 12-Mar-12 10:17am    
Sweet, I'm glad it worked for you. Although I don't recommend doing this at all, it looks like there something wrong with your design if you need to do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900