public void myMethod1(object obj)
the obj is not arrylist, but It may contains a arraylist
Main Topics
Browse All Topicsc# .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).IsAs
{
...
}
}
MyClass1 a = new MyClass(...); // MyClass contains a ArrayList
myMethod(a);
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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(IArrayListContai
{
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.
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
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.
Unless you use a common interface, you are going to have to loop through each of the properties using reflection to figure out which property holds the array list. However, this seems _very_ kludgey and could cause many problems: what if the class holds more that one array list? which one do you use?
If you have control of what classes can be passed in to myMethod1 I would _highly_ recommend using an interface so the classes have control of which array list is used rather than trying to "dig into" the class using reflection.
What dstanley9 is talking about is this:
// Declare an interface with the stuff that classes you can pass to your method have in common.
interface IHaveList {
public ArrayList myList;
}
// All classes that you can pass to the method, implement that interface:
class One : IHaveList {
public ArrayList myList = new ArrayList();
// other stuff
}
class Two : IHaveList {
public ArrayList myList = new ArrayList();
// other stuff
}
// The method receives IHaveList, instead of class One or Two..
class SomeClass {
void myMethod( IHaveList obj )
{
foreach( object o in obj.myList )
// Do something with the objects..
}
Any object that implements IHaveList can be passed as a parameter to myMethod()
NOTE: ArrayList is not a good thing to use any more. Much better to use generics to restrict what can go in the array, if possible (.NET 2.0, VS 2005).
Examples:
Array<One> xyz = new Array<One>(); // This array can only hold objects of class One
Array<IHaveList> = new Array<IHaveList>(); // This array can only hold objets that implement IHaveList
Dave
Business Accounts
Answer for Membership
by: dcgamesPosted on 2007-02-22 at 18:56:16ID: 18593441
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).