Link to home
Start Free TrialLog in
Avatar of yk99
yk99

asked on

c# Reflection Type

c# .net Programming question.
MyClass1 contains a ArrayList of Object,
How can I access it in myMethod1?

public void  myMethod1(object obj)
{
   Type type = obj.getType();
   if (!typeof(ICollection).IsAssignableFrom(type))
  {
  ...
  }
}

MyClass1 a = new MyClass(...); // MyClass contains a ArrayList
myMethod(a);

Avatar of dcgames
dcgames

If you know its supposed to be an array list, why not just use it as that?

        ArrayList x = obj as ArrayList;
        <use x like you normally use an array list>

I believe that if obj is NOT an ArrayList, then you either get an exception (so try/catch) or x will be null (I don't remember which of the two).
Avatar of yk99

ASKER

public void  myMethod1(object obj)

the obj is not arrylist, but It may contains a arraylist
Sounds like a perfect use for an interface:

public interface IArrayListContainer
{
  public ArrayList GetArrayList();
}

public class MyClass : IArrayListContainer
{
  #region IArrayListContainer implementation
  public ArrayLst GetArrayList()
  {
     // return the ArrayList that the class contains
  }
  #endregion
  // other class members
}

public void  myMethod1(IArrayListContainer obj)
{
  ArrayList al = obj.GetArrayList();
}

MyClass1 a = new MyClass(...); // MyClass contains a ArrayList
myMethod1(a);  // Can implicitly cast to an IArrayListContainer

Otherwise, you'd have to loop through all of the properties of a to find out which one returns an ArrayList.  Sounds _very_ kludgey to me.
Avatar of yk99

ASKER

sorry, I didn't make it clear.

public void  myMethod1(object obj)

the parameter obj 's type is object, not a specific class type.

for example
MyClassTest1;
MyClassTest2
myMethod1(Test1)
myMethod1(Test2)
....

when call this method, all the class in the parameter are parsed to object type.
Avatar of yk99

ASKER

simple, the question is  how to get an instance of an object from variable class name

ex:
string classname = "MyClass1";

object obj;

want to parse obj to the type of MyClass1

How to do it?
I think I get it..  You want toget at an array list that is inside the object, but the object itself may be different classes depending on when you call it..

abc = myMethod( obj1 );
def = myMethod( obj2 );

Where obj1 ad ojb2 are different classes, but both have the 'ArrayList' inside.

The easiest way is if the array list is exposed as a PROPERTY in both classes with the same name.

Mind you, I'm doing this from memory.. I don't have my home computer in front of me right now..

   Type type = obj.getType();
   PropertyInfo pi = type.GetProperty("MyList");
     if (pi != null) {
         // Found it.
        if( pi.PropertyType == typeof(ArrayList) ) {
            ArrayList myRef = (ArrayList)pi.GetValue(obj, BindingFlags.Getproperty,null,null,null);
           foreach( object in myRef  )
                // examine each object in the array list...
        }
    }

This works also for Methods, not just Properties. And if the name is not fixed, you can always get the LIST of properties, then iterate over them looking for the ArrayList..

   Type type = obj.getType();
  PropertyInfo[] piArray = type.GetProperties();
   for (int j = 0; j < piArray.GetLength(0); j++)
   {
           if( piArray[j].PropertyType == typeof(ArrayList) ) {
                 // Found an array list
   ...

Hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial