0

So.. I have some WCF services, and these wcf services are called with reflection. They return arrays with objects that are different for each service that gets called.

My mission is to get these objects and map them to objects that i have in my BusinessObjects. They are defined by the T.

    public virtual IQueryable<T> GetAll()
    {
        //Methods retreives an array of objects.
        var blah = _getAllFromWcf.Invoke(_rawServiceObject, new object[] {});
        //Says that this is an array
        var blahsType = blah.GetType();
        //This is the type of object in the array
        var blahsElementType = blahsType.GetElementType();
        //This is where i want to convert the element in the array to the type T so that i can return it in the IQueryable<T>
        blah.MapCollection<'The type of element in blah', T>();

        return null;
    }

The blah.MapCollection<> is an extension method i have made that uses AutoMapper and converts elements in lists.

The MapCollection will of course not work right now, because it does not understand that blah is an array, and 'The type of element in blah' is not working, because i dont know the type of the object at this time....

Anyone has any guidance?

Bjørn
  • 1,138
  • 2
  • 16
  • 47
  • 1
    You can continue using reflection and Invoke MapCollection method: http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method. – qbik Jun 16 '14 at 12:04
  • Good idea.. but i still need to return the collection as an IQueryable... any hints for that? – Bjørn Jun 16 '14 at 12:19

2 Answers2

1

Your extension method cannot use the type as generic parameter because it's not known at runtime. You will have to pass it as normal parameter of type Type to your extension. AutoMapper offers methods to pass the type information as normal parameters also.

You can also just use LinQ to map with AutoMapper:

blah.Select(item => Mapper.Map(item, item.GetType(), typeof(T)) as T)
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • But i still have to be able to tell Automapper that the object it recieves is a list/array/ienumerable of some sort, if not it will try to map it as a single object, and that will not work.. so how do i do that? – Bjørn Jun 16 '14 at 12:04
  • @BjørnØyvindHalvorsen Not necessarily, you can do the container conversion yourself and let automapper worry about the item to item conversion. Please see my edit. – nvoigt Jun 16 '14 at 12:23
  • That is very good! thank you.. i ended up doing it a little bit different tho.... i will post my proposed solution.. :) – Bjørn Jun 16 '14 at 12:25
0

I ended up doing it like this.. if you have comments on it, or just have a better way of doing it, please feel free to comment :)

    public virtual IQueryable<T> GetAll()
    {
        //Methods retreives an array of objects.
        var collectionFromWcfService = _getAllFromWcf.Invoke(_rawServiceObject, new object[] {});
        //Says that this is an array
        var typeOfArray = collectionFromWcfService.GetType();
        //This is the type of object in the array
        var elementTypeInArray = typeOfArray.GetElementType();

        MethodInfo method = typeof(Extensions).GetMethod("MapCollectionWithoutExtension");
        MethodInfo generic = method.MakeGenericMethod(elementTypeInArray,typeof(T));

        var convertedListOfObjects = (List<T>)generic.Invoke(this, new []{ collectionFromWcfService });

        return convertedListOfObjects.AsQueryable(); 
    }

With this solution i have hidden away the AutoMapper implementation and can now do the conversion by another tool, or manually if i need it at a later point in time.

Bjørn
  • 1,138
  • 2
  • 16
  • 47