How to get property name?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

if I have a public static property such as:

//Class1
public class Class1
{
public static Dictionary<string, string> Prop1{...}
public static Dictionary<string, string> Prop2{...}
}

//SomeClass
public class SomeClass
{
public SomeClass ( CustomDictionary<string, string> pdict )
{//get name of pdict here}
}

// usage
SomeClass myProp1 = new SomeClass( Class1.Prop1 );

How can I get the name of pdict in SomeClass()? Meaning, I want a
string containing "Prop1".

Thanks,
Brett
 
Brett said:
if I have a public static property such as:

//Class1
public class Class1
{
public static Dictionary<string, string> Prop1{...}
public static Dictionary<string, string> Prop2{...}
}

//SomeClass
public class SomeClass
{
public SomeClass ( CustomDictionary<string, string> pdict )
{//get name of pdict here}
}

// usage
SomeClass myProp1 = new SomeClass( Class1.Prop1 );

How can I get the name of pdict in SomeClass()? Meaning, I want a
string containing "Prop1".

You can't unless you provide a mechanism for them to specifically tell you.
You are getting a reference to an object of type
CustomDictionary<string,string>. You have no way of know that the object
was originally created in the static get_Prop1. What if get_Prop1 had
delegated the creation of its type to some different method? What would you
want to get?

BTW, your code seems upside down. I would expect a CustomDictionary to
derive from Dictionary. If that is the case, a constructor expecting a
CustomDictionary will not be able to accept an object of type Dictionary.
 
Use TypeDescriptor.GetProperties() - take a look at Help. A property
descriptor collection will be returned. You can look up various values of a
property, including its Name.

Hope this helps.

-=Gary
 
Sorry Tom. I meant to put Dictionary.
Use TypeDescriptor.GetProperties() - take a look at Help. A property
descriptor collection will be returned. You can look up various values of a
property, including its Name.

I've tried the msdn example for this and still don't get the name I'm
looking for. I will just define the property names in static strings.

Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top