Help! Reflection object target in c#?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nigil LaVey (lavey_at_pacific.net.sg)
Date: 03/23/04


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();
 }
}



Relevant Pages

  • nested classes, OOP
    ... Is there a way to implement nested classes in MATLAB? ... if I have two separate classes "myClass" and ... function = mySubClass() ... but this violates the philosophy of encapsulation. ...
    (comp.soft-sys.matlab)
  • Re: Understanding class access
    ... The people who are going to use my extension will extend MyClass. ... declare it to be public. ... But I want MyClass to be able to access MySubClass, ... MySubClass invisible for packages outside mycompany.mypackage? ...
    (comp.lang.java.programmer)
  • Re: Understanding class access
    ... The people who are going to use my extension will extend MyClass. ... declare it to be public. ... But I want MyClass to be able to access MySubClass, ... MySubClass invisible for packages outside mycompany.mypackage? ...
    (comp.lang.java.programmer)
  • Re: How to call the subclasss method?
    ... If the method is unique to the subclass you may not want it virtual as other ... > I created a class "myClass" and another class "mySubclass" which inherited ... > but I got an error message when I tried to compile it: ...
    (microsoft.public.dotnet.languages.csharp)