Regarding Reflections
-
How to gate the values of some properties in the class Like I have a Class ... Class1 in which some properties are defined say FName, LName.. Now what I have to recieve is the values that are stores in these Properties and then write them in comma separated form in a text file How can I do this? Parag Gupta
-
How to gate the values of some properties in the class Like I have a Class ... Class1 in which some properties are defined say FName, LName.. Now what I have to recieve is the values that are stores in these Properties and then write them in comma separated form in a text file How can I do this? Parag Gupta
define these variables as public static string FName; inside the class, outside any functions. Then you should be able to just get the value from anywhere by going: Class1.FName Im not sure if this is a good way of doing it or whatever, but i do it all the time, and i've never had any problems :)
-
How to gate the values of some properties in the class Like I have a Class ... Class1 in which some properties are defined say FName, LName.. Now what I have to recieve is the values that are stores in these Properties and then write them in comma separated form in a text file How can I do this? Parag Gupta
If you want to use reflection (as by your title) then
object value = instanceOfClass.GetType().GetProperty("FName").GetValue(instanceOfClass, null);
That doesn't do anything different thanobject value = instanceOfClass.FName;
However if you want a generic routine for saving all properties then you can do something like the following:foreach (PropertyInfo pi in instanceOfClass.GetType().GetProperties())
{
object value = pi.GetValue(instanceOfClass, null);
// Do what you want with value - e.g. save it to a file
}
If you're stuck in a rut: 1) Consult the documentation* 2) Google it 3) Ask a sensible question 4) Try an ancient ritualistic knowledge summoning (:badger::badger::badger:) dance :jig: 5) Phone :bob: * - If the documentation is MSDN > 6.0 then forget it!
-
If you want to use reflection (as by your title) then
object value = instanceOfClass.GetType().GetProperty("FName").GetValue(instanceOfClass, null);
That doesn't do anything different thanobject value = instanceOfClass.FName;
However if you want a generic routine for saving all properties then you can do something like the following:foreach (PropertyInfo pi in instanceOfClass.GetType().GetProperties())
{
object value = pi.GetValue(instanceOfClass, null);
// Do what you want with value - e.g. save it to a file
}
If you're stuck in a rut: 1) Consult the documentation* 2) Google it 3) Ask a sensible question 4) Try an ancient ritualistic knowledge summoning (:badger::badger::badger:) dance :jig: 5) Phone :bob: * - If the documentation is MSDN > 6.0 then forget it!
Sorry, but that is not what exactly I want What I want is that I want to apply this thing to many classes of which I don't know the properties that it contains as in.... in class1 it may be FName or LName but what is in class 2 I don't know I want to have some common code that will apply for all the classes I want. the problem with this one is that here I have to call the values of all the properties individually Please reply. Thanks in Advance
-
Sorry, but that is not what exactly I want What I want is that I want to apply this thing to many classes of which I don't know the properties that it contains as in.... in class1 it may be FName or LName but what is in class 2 I don't know I want to have some common code that will apply for all the classes I want. the problem with this one is that here I have to call the values of all the properties individually Please reply. Thanks in Advance
Simple sample method:
public void SaveObject(object value)
{
PropertyInfo[] properties = value.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
SaveValue(propertyInfo.GetValue(value, null));
}
}Now you just call SaveObject with whatever object you want to save. Note that
SaveValue
will have to inspect the object to see whether it's a primitive type or a complex (e.g.TextBox
) and decide how to save it. There is a property namedIsPrimitive
on theType
object I think which you can use to decide what to. do.
If you're stuck in a rut: 1) Consult the documentation* 2) Google it 3) Ask a sensible question 4) Try an ancient ritualistic knowledge summoning (:badger::badger::badger:) dance :jig: 5) Phone :bob: * - If the documentation is MSDN > 6.0 then forget it!