473,703 Members | 2,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6802
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.St atic.

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.goo glegroups.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(someclas s);
System.Reflecti on.FieldInfo field= t.GetField("thi ng",
System.Reflecti on.BindingFlags .Public |
System.Reflecti on.BindingFlags .Static);
MessageBox.Show ("value is " + (string)field.G etValue(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.St atic.

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.goo glegroups.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("Ta bleName" );
return TableNameInfo[0].ToString();
}

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

So then I tried this:

private static string GetTableName( Type t ){
MemberInfo[] TableNameInfo =
t.GetMember("Ta bleName",Member Types.All, BindingFlags.St atic );

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(someclas s);
System.Reflecti on.FieldInfo field= t.GetField("thi ng",
System.Reflecti on.BindingFlags .Public |
System.Reflecti on.BindingFlags .Static);
MessageBox.Show ("value is " + (string)field.G etValue(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.G etValue( 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(someclas s);
System.Reflecti on.FieldInfo field= t.GetField("thi ng",
System.Reflecti on.BindingFlags .Public |
System.Reflecti on.BindingFlags .Static);
MessageBox.Show ("value is " + (string)field.G etValue(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.G etValue( 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
451
by: Andre | last post by:
I have two questions; 1) When executing the sub routine "TestStructure", I'm trying to update the member "intTyres" in the structure "structureCar" dynamically using System.Reflection. The code executes, but the value does not change, nor is there an exception thrown. 2) Read comments in sub routine "TestIntegerProperty"
3
5079
by: J E E | last post by:
Hi! Is it possible to access fields in a derived class using reflection? Code below works fine when I access it as a private member in the Page class, but not when accessing base class member through an interface reference. I have tried to change the snd argement to SetAttribute method from 'Name', 'set_Name' to '_name'. That doesn't seem to be the problem. I
0
1716
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am sure many others have) using reflection. Here is some sample code that will print out the auto...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am sure many others have) using reflection. Here is some sample code that will print out the auto...
9
6236
by: QQ | last post by:
I am trying to use reflection to access the private property of OpenFileDialog fileNames property. I have the following code : filedlg.InitialDirectory = directory.Text; filedlg.Filter = "xml files (*.xml)|*.xml" ; filedlg.FileName = "*.xml"; filedlg.ValidateNames = false; if (filedlg.ShowDialog() == DialogResult.Cancel)
9
2390
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure that I use to communicate to some unmanaged code in a DLL written in C. It is not complicated, but will change and I would like to be able to sequentially access it without explicitly referring to each and every element. Here is the structure:
2
2661
by: mswlogo | last post by:
I looked high and low for code to do this and finally found some VB code that did it right. This is a C# flavor of it. public event EventHandler<EventArgsMyEventToBeFired; public void FireEvent(Guid instanceId, string handler) { EventArgs e = new EventArgs(instanceId);
10
3381
by: =?Utf-8?B?QnJpYW4=?= | last post by:
What is the easiest way to convert primitives to a byte array? I tried the BinaryFormatter serialization but it serializes objects so when I serialized an and int it took 54 bytes instead of 4. I tried something like the following but it won't compile (I admit I use generics a lot but haven't written many so this may be way off): class X<T> { public byte ToByteArray(T val) {
6
2932
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class TestClass { public int i = 0; public int IValue
0
8755
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8667
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9116
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9013
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4428
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2445
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.