472,141 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

using reflection to read the value of a public static member

I have several classes that all have the same static member:

class A{
public static string Table = "TableA";
}

class B{
public static string Table = "TableB";
}

none of the classes inherit from a common base class or interface.

How can I use reflection to get the value stored in A.Table and
B.Table?
I found examples that called static member functions but none that
accessed non-function/non-property.

I'm simply trying associate a string with a class and not with a
particular instance of that class. I could create an interface but I
don't want to have to create an instance just to access a static
member.

thanks.
Dean

Nov 17 '05 #1
5 6676
Hi there... You can use the Type.GetMember method.
http://msdn.microsoft.com/library/de...embertopic.asp
When specifying the binding flags remember to add BindingFlags.Static.

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com


<re******@gmail.com> escribió en el mensaje
news:11**********************@g49g2000cwa.googlegr oups.com...
I have several classes that all have the same static member:

class A{
public static string Table = "TableA";
}

class B{
public static string Table = "TableB";
}

none of the classes inherit from a common base class or interface.

How can I use reflection to get the value stored in A.Table and
B.Table?
I found examples that called static member functions but none that
accessed non-function/non-property.

I'm simply trying associate a string with a class and not with a
particular instance of that class. I could create an interface but I
don't want to have to create an instance just to access a static
member.

thanks.
Dean

Nov 17 '05 #2
class someclass
{
public static string thing = "hey u!";

}
private void readvalue()
{
Type t = typeof(someclass);
System.Reflection.FieldInfo field= t.GetField("thing",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static);
MessageBox.Show("value is " + (string)field.GetValue(new
someclass()));
}

Abubakar.

"re******@gmail.com" wrote:
I have several classes that all have the same static member:

class A{
public static string Table = "TableA";
}

class B{
public static string Table = "TableB";
}

none of the classes inherit from a common base class or interface.

How can I use reflection to get the value stored in A.Table and
B.Table?
I found examples that called static member functions but none that
accessed non-function/non-property.

I'm simply trying associate a string with a class and not with a
particular instance of that class. I could create an interface but I
don't want to have to create an instance just to access a static
member.

thanks.
Dean

Nov 17 '05 #3

Angel J. Hernández M. wrote:
Hi there... You can use the Type.GetMember method.
http://msdn.microsoft.com/library/de...embertopic.asp
When specifying the binding flags remember to add BindingFlags.Static.

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com


<re******@gmail.com> escribió en el mensaje
news:11**********************@g49g2000cwa.googlegr oups.com...
I have several classes that all have the same static member:

class A{
public static string Table = "TableA";
}

class B{
public static string Table = "TableB";
}

none of the classes inherit from a common base class or interface.

How can I use reflection to get the value stored in A.Table and
B.Table?
I found examples that called static member functions but none that
accessed non-function/non-property.

I'm simply trying associate a string with a class and not with a
particular instance of that class. I could create an interface but I
don't want to have to create an instance just to access a static
member.

thanks.
Dean


Angel,

I already tried using GetMember like this:

private static string GetTableName( Type t ){
MemberInfo[] TableNameInfo = t.GetMember("TableName" );
return TableNameInfo[0].ToString();
}

but the function returned: "string ClassName.TableName" instead of the
value stored in it.

So then I tried this:

private static string GetTableName( Type t ){
MemberInfo[] TableNameInfo =
t.GetMember("TableName",MemberTypes.All, BindingFlags.Static );

return TableNameInfo[0].ToString();

}

and got no members returned from the GetMembers() function.
After I get the MemberInfo object, how do I get the static value
associated with it. I was looking for a GetValue() function but
couldn't find one.

thanks,
Dean Rettig

Nov 17 '05 #4

Abubakar wrote:
class someclass
{
public static string thing = "hey u!";

}
private void readvalue()
{
Type t = typeof(someclass);
System.Reflection.FieldInfo field= t.GetField("thing",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static);
MessageBox.Show("value is " + (string)field.GetValue(new
someclass()));
}

Abubakar.

"re******@gmail.com" wrote:

Abubakar,

Thanks! This worked! But I don't want to create an instance of the
object so I changed it to:

MessageBox.Show( "value is " + (string)field.GetValue( null ) );

and it worked just fine.

Dean

I have several classes that all have the same static member:

class A{
public static string Table = "TableA";
}

class B{
public static string Table = "TableB";
}

none of the classes inherit from a common base class or interface.

How can I use reflection to get the value stored in A.Table and
B.Table?
I found examples that called static member functions but none that
accessed non-function/non-property.

I'm simply trying associate a string with a class and not with a
particular instance of that class. I could create an interface but I
don't want to have to create an instance just to access a static
member.

thanks.
Dean


Nov 17 '05 #5
hey thanks! Actually I didnt know that I could pass to "GetValue" a "null" so
i just created a dumb object :-). People even learn when they answer
questions :)

Abubakar.
http://joehacker.blogspot.com

"re******@gmail.com" wrote:

Abubakar wrote:
class someclass
{
public static string thing = "hey u!";

}
private void readvalue()
{
Type t = typeof(someclass);
System.Reflection.FieldInfo field= t.GetField("thing",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static);
MessageBox.Show("value is " + (string)field.GetValue(new
someclass()));
}

Abubakar.

"re******@gmail.com" wrote:

Abubakar,

Thanks! This worked! But I don't want to create an instance of the
object so I changed it to:

MessageBox.Show( "value is " + (string)field.GetValue( null ) );

and it worked just fine.

Dean

I have several classes that all have the same static member:

class A{
public static string Table = "TableA";
}

class B{
public static string Table = "TableB";
}

none of the classes inherit from a common base class or interface.

How can I use reflection to get the value stored in A.Table and
B.Table?
I found examples that called static member functions but none that
accessed non-function/non-property.

I'm simply trying associate a string with a class and not with a
particular instance of that class. I could create an interface but I
don't want to have to create an instance just to access a static
member.

thanks.
Dean


Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by QQ | last post: by
2 posts views Thread by mswlogo | last post: by
10 posts views Thread by =?Utf-8?B?QnJpYW4=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.