user4189129
user4189129

Reputation:

How to be working with a generic IEnumerable<T>?

I have written a function which will take a generic IEnumerable and map these objects (if possible) to objects I want to use for an algorithm. I don't have a lot of experience with generic objects or IEnumerables, so I wanted to ask for some advice.

I have written this code:

public static IEnumerable<OtherObject> MyObjectToOtherObject<T>(IEnumerable<T> objects)
{
    if (objects.GetType() == typeof(MyObject))
    {
        var data = (IEnumerable<MyObject>)objects;
        return data.Select(x => new OtherObject() { // Map the attributes });
    }
    else
        return null;
}

This doesn't work in my code, since it returns null even though the input list is of the type MyObject. I just can't figure out how to cast my IEnumerable<T> to IEnumerable<MyObject>. Is there something I'm doing wrong?

Thanks in advance!

I'm sorry for all the confusion. I have asked this the wrong way. Alex has helped me enough, thank you!

Upvotes: 1

Views: 938

Answers (4)

Alex Lebedev
Alex Lebedev

Reputation: 609

this is wrong.

objects.GetType() == typeof(MyObject)

Correct:

objects.GetType() == typeof(IEnumerable<T>)

If you need to compare type of nested items:

objects.GetType().GetGenericArguments()[0] == typeof(MyObject)
typeof(T) == typeof(MyObject)

If you are checking if the type can be casted to another type:

objects.GetType().GetGenericArguments()[0].IsAssignableFrom(typeof(MyObject))

or just as

typeof(T).IsAssignableFrom(typeof(MyObject))

Upvotes: 1

nozzleman
nozzleman

Reputation: 9649

objects will never be of type MyObject, therefore

(objects.GetType() == typeof(MyObject))

will always return false because object is some generic collection (IEnumerable<T>).

you could try using

typeof(T) == typeof(MyObject)

Upvotes: 0

Jamiec
Jamiec

Reputation: 136074

I think what you're trying to do is build a generic mapping method which can map any give T to your concrete class Object1. You need to provide a mapping function which does the mapping.

Say you have this (overly contrived) example:

public class SourceObject
{
    public string AString{get;set;}
}

public class DestObject
{
    public string TheProperty{get;set;}
}

And you start with an IEnumerable<SourceObject> your method would need to take the list, and a Func<SourceObject,DestObject> to do the mapping

var input = new List<SourceObject>(){
    new SourceObject{AString="Str1"},
    new SourceObject{AString="Str2"}
};

var result = MyObjectToOtherObject(input, x =>  new DestObject{TheProperty = x.AString});

This is accomplished like so:

public static IEnumerable<DestObject> MyObjectToOtherObject<T>(IEnumerable<T> objects, Func<T,DestObject> mapping)
{
    return data.Select(mapping);
}

As you can see, the separate method is more-or-less useless at this point. It's exactly what Select does.

Upvotes: 0

KMoussa
KMoussa

Reputation: 1578

Doesn't look like you need a generic method, if you always know the source and destination types, you can do this:

public static IEnumerable<DestObject> ObjectToObject1(IEnumerable<SourceObject> objects)
{                
    return data.Select(x => new DestObject() { // Map the attributes });

}

Upvotes: 0

Related Questions