3

I have 2 types :

    public class Type1
    {
        public string Name { get; set; }
    }

    public class Type2
    {
        public string Name { get; set; }
    }

I have a list of elements (each element is an object type). Some elements could be an array. (an array could be a type1[] or a type2[])

My goal is to :
1-iterate on my list of elements
2-determine which are type1[]array pr type2[] array
3-get the Name value property for element of those previous array

This is what I have done :

    foreach (var Myobject in MyList)
    {
        if (myObject.GetType().IsArray)
        {
            var elementType = myObject.GetType().GetElementType()// should me return the element type, ie Type1 or Type2

            //This is where I am stuck, I know that my object is an array but I cannot cast if in type1[] or type2[] array by using elementType
            //The following is not working
            elementType[] myArrat = (elementType[])myObject;

            // And I don't want to hardwrite each case for each possible type like this :
            Type1[] myArrat = (Type1[])myObject;
            Type2[] myArrat = (Type2[])myObject;
            // I want to use the elementType that I got previously

        }
    }

Thanks in advance for your help.

2 Answers 2

1

You can't do what you are trying to do. And quite frankly, you probably don't need to do it either. If you are expecting different types it means you are going to do different things with each type. What you can do is to change Type1 and Type2 to extend the same base class and use the base class instead:

public class TypeBase 
{
   public virtual string Name { get; set; }
}

public class Type1 : TypeBase
{
}

public class Type2 : TypeBase
{
}


foreach (var myobject in MyList)
{
    if (myObject.GetType().IsArray)
    {
        object[] array = (object[]) myObject;
        TypeBase[] yourArray = array.Cast<TypeBase>();
        //use the properties and methods of TypeBase instead of Type1 and Type2
        //mark the methods and properties in TypeBase as virtual and
        //override them on Type1 and Type2 if needed
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

elementType[] myArrat = (elementType[])myObje

elementTypr is not type name then it won't compile. However there are few other issues here. First of all you may want to loop through MyList elements to flat array items:

private static IEnumerable<object> Flat(IEnumerable<object> items)
{
    foreach (var item in items)
    {
        var array = item as Array;
        if (array != null)
        {
            foreach (var arrayItem in array)
                yield return arrayItem;
        }
        else
            yield return item;
    }
}

Now you can enumerate through your list without worrying if some items are an array or not:

foreach (var item in Flat(Mylist))
{
}

What you should really do is to add an interface (or an abstract base class) to abstract concrete type:

interface INamedObject
{
    string Name { get; set; }
}

class Type1 : INamedObject { ... }
class Type2 : INamedObject { ... }

Now you can replace object with INamedObject in Flat() return type (don't forget to add a cast for yield return). You will then use it like this:

foreach (var item in Flat(Mylist))
{
    item.Name = ""; // It doesn't matter if it's Type1 or Type2!
}

Note that if you don't want (!!!) to add a base class/interface then you may still perform a type checking. It's a very bad practice and you should really consider to change your design to avoid that. What I'd suggest is to - at least - to do not use GetType() but directly as casting operator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.