Muhammad Nour
Muhammad Nour

Reputation: 2327

How to get the entities from generic EntitySet on run-time without knowing the generic type?

I am creating a mapper function that will take EntitySet<> with unknown different generic types at the runtime and I want to get the entities inside the incoming EntitySet and add them to a List.

I can't access the entities because I am having the EntitySet coming as an Object and when I try to cast the object to the respective type I fail because I don't know the generic type of the EntitySet

((EntitySet<XXXX>)dataValues[pInfo.Name]).ToList();

I can read the datatype of the generic EntitySet but I can't use it to make the cast, I don't know how or even if it is possible or not!

dataValues[pInfo.Name].GetType().GenericTypeArguments.First()

I don't care much about the generic type since I just need to get the collection inside the EntitySet only.

I know my question looks like repeated, but I honestly couldn't solve my problem with the results I found when I made the search.

I am using C# and ASP.net

Upvotes: 0

Views: 65

Answers (1)

Gy&#246;rgy Guly&#225;s
Gy&#246;rgy Guly&#225;s

Reputation: 1598

The EntySet is inherited from System.Collections.Generic.ICollection the System ICollection is inhertted from System.Collections.Generic.IEnumerable and IEnumerable is inherited from System.Collections.IEnumerable.

The System.Collections.IEnumerable is not generic interface. and you can use in in foreach.

So your function is like this:

public class Example
{
     public static void EnumEntities( IEnumerable entities )
     {
        foreach( var entity in entities )
        {
            Console.WriteLn( entity.ToString());
        }
     }
}

Upvotes: 1

Related Questions