Help! Reflection object target in c#?

  • Thread starter Thread starter Nigil LaVey
  • Start date Start date
N

Nigil LaVey

hi guys,

I have the following problems with object target.. noticed the " // Help!. "
at the GetValue statement below..
Can I reference the target object by it type?

using System;
using System.Reflection;

public class MySubClass
{
private string str_Text="somevaluesome";
public String Text
{
get { return str_Text; }
set { str_Text=value; }
}
}

public class MyClass
{
private string str_Text="somevalue";
private MySubClass obj_MySubClass=new MySubClass();

public MyClass()
{
Type obj_Type=GetType();
PropertyInfo[] obj_PropertyInfo =
obj_Type.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Text="somevaluechanged"; // set the value and write it out to see if
current instance the same.
obj_MySubClass.Text="somevaluesomechanged"; // set the value and write it
out to see if current instance the same.

foreach(PropertyInfo Property in obj_PropertyInfo)
{
if(Property.Name=="SubClass")
{
Type obj_SubType = Property.PropertyType;
PropertyInfo[] obj_SubPropertyInfo =
obj_SubType.GetProperties(BindingFlags.Public|BindingFlags.NonPublic|Binding
Flags.Instance);

foreach(PropertyInfo SubProperty in obj_SubPropertyInfo)
{
Console.WriteLine("Name : "+SubProperty.Name);
Console.WriteLine("Value : "+SubProperty.GetValue(this,null)); //
comment either one to this the effect. this one not working
// Console.WriteLine("Value : "+SubProperty.GetValue(SubClass,null)); //
comment either one to this the effect. this one working
// Help!. Can I reference SubClass as object by it type?
}
}
else
{
Console.WriteLine("=========================================");
Console.WriteLine("Name : "+Property.Name);
Console.WriteLine("Value : "+Property.GetValue(this,null));
Console.WriteLine("=========================================");
}
}
}

public MySubClass SubClass
{
get { return obj_MySubClass; }
set { obj_MySubClass=value; }
}

public String Text
{
get { return str_Text; }
set { str_Text=value; }
}

}
public class MainClass
{
public static void Main()
{
MyClass obj_Class = new MyClass();
}
}
 
Nigil LaVey said:
I have the following problems with object target.. noticed the " // Help!. "
at the GetValue statement below..
Can I reference the target object by it type?

I'm not really sure what you're trying to do - but you can't use the
property of one class against an instance of another class, which is
what you're trying to do with the failing line. "this" is an instance
of MyClass, but the property you're trying to use is a property of the
MySubClass class.
 
hi Jon,

thanks for ur reply... appreciates it..

ok.. cos I thought that by doing so.. my control will be much more dynamic..
rite now I am just using those easy way out which is..

private object objTarget(string str_Target)
{
switch(str_Target)
{
case "Button.Login":
return Button.Login
break;
case "Button.Reset":
return Button.Reset
break;
}
}

SubProperty.GetValue(objTarget(SubProperty.Name),null));

and I have searched through the net.. the closest to this is I reckon is one
of the undocumented keywords and type
http://www.eggheadcafe.com/articles/20030114.asp

int i = 21;
TypedReference tr = __makeref(i);

Type t= __reftype(tr);
Response.Write( t.ToString());

with the above statement.. can I make a typereference for class variables?
cos there'a method to convert it to object in TypedReference.ToObject

Regards,
LaVey
 
Nigil LaVey said:
thanks for ur reply... appreciates it..

ok.. cos I thought that by doing so.. my control will be much more dynamic..
rite now I am just using those easy way out which is..

private object objTarget(string str_Target)
{
switch(str_Target)
{
case "Button.Login":
return Button.Login
break;
case "Button.Reset":
return Button.Reset
break;
}
}

SubProperty.GetValue(objTarget(SubProperty.Name),null));

and I have searched through the net.. the closest to this is I reckon is one
of the undocumented keywords and type
http://www.eggheadcafe.com/articles/20030114.asp

int i = 21;
TypedReference tr = __makeref(i);

Type t= __reftype(tr);
Response.Write( t.ToString());

with the above statement.. can I make a typereference for class variables?
cos there'a method to convert it to object in TypedReference.ToObject

I wouldn't use that, myself. Basically, the above looks like you need
to look up two properties - first "Button", then get the value of that,
the type of the value, and then the "Login" or "Reset" property from
that type.
 
Back
Top