Help! Reflection object target in c#?
From: Nigil LaVey (lavey_at_pacific.net.sg)
Date: 03/23/04
- Next message: R. Pavey: "MAPI reference missing in VB.Net"
- Previous message: Vipul Patel: "RE: Including or importing Source files?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Help! Reflection object target in c#?"
- Reply: Jon Skeet [C# MVP]: "Re: Help! Reflection object target in c#?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 23 Mar 2004 12:44:31 +0800
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();
}
}
- Next message: R. Pavey: "MAPI reference missing in VB.Net"
- Previous message: Vipul Patel: "RE: Including or importing Source files?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Help! Reflection object target in c#?"
- Reply: Jon Skeet [C# MVP]: "Re: Help! Reflection object target in c#?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|