Connecting Tech Pros Worldwide Forums | Help | Site Map

using reflection to read the value of a public static member

rettigcd@gmail.com
Guest
 
Posts: n/a
#1: Nov 17 '05
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 J. Hernández M.
Guest
 
Posts: n/a
#2: Nov 17 '05

re: using reflection to read the value of a public static member


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






<rettigcd@gmail.com> escribió en el mensaje
news:1126665486.192412.314970@g49g2000cwa.googlegr oups.com...[color=blue]
>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
>[/color]


Abubakar
Guest
 
Posts: n/a
#3: Nov 17 '05

re: using reflection to read the value of a public static member


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.

"rettigcd@gmail.com" wrote:
[color=blue]
> 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
>
>[/color]
rettigcd@gmail.com
Guest
 
Posts: n/a
#4: Nov 17 '05

re: using reflection to read the value of a public static member



Angel J. Hernández M. wrote:[color=blue]
> 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
>
>
>
>
>
>
> <rettigcd@gmail.com> escribió en el mensaje
> news:1126665486.192412.314970@g49g2000cwa.googlegr oups.com...[color=green]
> >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
> >[/color][/color]

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

rettigcd@gmail.com
Guest
 
Posts: n/a
#5: Nov 17 '05

re: using reflection to read the value of a public static member



Abubakar wrote:[color=blue]
> 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.
>
> "rettigcd@gmail.com" wrote:
>[/color]
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

[color=blue][color=green]
> > 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
> >
> >[/color][/color]

Abubakar
Guest
 
Posts: n/a
#6: Nov 17 '05

re: using reflection to read the value of a public static member


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

"rettigcd@gmail.com" wrote:
[color=blue]
>
> Abubakar wrote:[color=green]
> > 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.
> >
> > "rettigcd@gmail.com" wrote:
> >[/color]
> 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
>
>[color=green][color=darkred]
> > > 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
> > >
> > >[/color][/color]
>
>[/color]
Closed Thread