R
Roger Webb
Hey All,
I've got a class that I'm trying to get a listing of the properties...kinda
like one of the Reflection examples from Microsoft. However, unlike their
example, I need to get the value as well. And am having trouble
understanding how the GetValue() function of the PropertyInfo should be
setup as below.
Also, with it coming back as an object, is there a way to get it recast so
that the DoSomething function will work?
I'm hopeing/thinking/praying.. that there is a way to make the code below
have StringBuilder1.ToString() be
"-FirstField='String1'-SecondField=2"
Any help ... or comments would be appreciated.
- Roger
class LocalClass
{
public string FirstField;
public int SecondField;
}
<snip most of procedure>
....
LocalClass LocalClass1 = new LocalClass();
LocalClass1.FirstField = "String1";
LocalClass1.SecondField = 2;
Type DRType = LocalClass1.GetType();
PropertyInfo[] PropInfo = DRType.GetProperties();
foreach(PropertyInfo prop in PropInfo)
{
if(!(prop.PropertyType.IsArray))
{
StringBuilder1.Append("-"+DoSomething(prop.Name,
prop.GetValue(prop, ????)));
}
}
....
public string DoSomething(string A, string B)
{
return string.Format("{0}='{1}'",A,B);
}
public string DoSomething(string A, int B)
{
return string.Format("{0}={1}",A,B.ToString());
}
I've got a class that I'm trying to get a listing of the properties...kinda
like one of the Reflection examples from Microsoft. However, unlike their
example, I need to get the value as well. And am having trouble
understanding how the GetValue() function of the PropertyInfo should be
setup as below.
Also, with it coming back as an object, is there a way to get it recast so
that the DoSomething function will work?
I'm hopeing/thinking/praying.. that there is a way to make the code below
have StringBuilder1.ToString() be
"-FirstField='String1'-SecondField=2"
Any help ... or comments would be appreciated.
- Roger
class LocalClass
{
public string FirstField;
public int SecondField;
}
<snip most of procedure>
....
LocalClass LocalClass1 = new LocalClass();
LocalClass1.FirstField = "String1";
LocalClass1.SecondField = 2;
Type DRType = LocalClass1.GetType();
PropertyInfo[] PropInfo = DRType.GetProperties();
foreach(PropertyInfo prop in PropInfo)
{
if(!(prop.PropertyType.IsArray))
{
StringBuilder1.Append("-"+DoSomething(prop.Name,
prop.GetValue(prop, ????)));
}
}
....
public string DoSomething(string A, string B)
{
return string.Format("{0}='{1}'",A,B);
}
public string DoSomething(string A, int B)
{
return string.Format("{0}={1}",A,B.ToString());
}