2

When querying entity framework for all objects of a given type, you'd do something like this:

List<MyDesiredObjects> = _myContext.MyDesiredObjects.ToList();

But is there a way one can create a function to query EF to find all objects of a given type if the type is variable? I've created this function, which at least compiles:

private List<TEntity> GetFromEF<TEntity>() where TEntity : class
{
    MyDBEntities context = new UnityDBEntities(_entityConnection);

    IObjectContextAdapter adapter = (IObjectContextAdapter)context;
    System.Data.Objects.ObjectContext oContext = adapter.ObjectContext;

    return oContext.CreateObjectSet<TEntity>().ToList();
}

But I'm struggling to see how I can call it, never mind actually extract data from it. Using reflection, like this (ucm.MappingType is a Type):

MethodInfo method = typeof(BaseXmlReader).GetMethod("GetFromEF");
MethodInfo gMethod = method.MakeGenericMethod(  ucm.MappingType.GetType() );
var meh = gMethod.Invoke(null, null);

Fails with object reference not set to instance of object. And I've not even got to paging my list yet.

Any other way to approach this problem?

15
  • 2
    Where are you getting the exception? Is it when you are invoking the method or in GetFromEF? Commented Nov 18, 2013 at 15:10
  • It's on method.MakeGenericMethod - but I just spotted that's because method is null. Commented Nov 18, 2013 at 15:12
  • 1
    One way to call a generic method is like this: var list = GetFromEF<MyDesiredObjects>(); No need to get into reflection. msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx Commented Nov 18, 2013 at 15:20
  • 1
    You will probably need to provide these binding flags to GetMethod to enable it to get your private method BindingFlags.NonPublic | BindingFlags.Instance Commented Nov 18, 2013 at 15:21
  • 1
    The first parameter of the invoke needs to be an instance of BaseXmlReader, you cannot invoke on a null instance Commented Nov 18, 2013 at 15:31

1 Answer 1

1

To address your issue of having a null MethodInfo method:

GetMethod will only search for public members. You can change your method to public, or include nonpublic methods like this:

MethodInfo method = typeof(BaseXmlReader)
                       .GetMethod("GetFromEF", 
                                  BindingFlags.Instance | BindingFlags.NonPublic);

Also, I'm assuming that your GetFromEF method is on a class called BaseXmlReader? If not, that needs to be replaced with the class name.

Then you can invoke like this:

BaseXmlReader instance = new BaseXmlReader();
MethodInfo method = typeof(BaseXmlReader)
                       .GetMethod("GetFromEF", 
                                  BindingFlags.Instance | BindingFlags.NonPublic);
MethodInfo gMethod = method.MakeGenericMethod(typeof(ucm.MappingType));
var result = gMethod.Invoke(instance, null);

or like this if being called from inside itself:

MethodInfo method = typeof(BaseXmlReader)
                       .GetMethod("GetFromEF", 
                                  BindingFlags.Instance | BindingFlags.NonPublic);
MethodInfo gMethod = method.MakeGenericMethod(typeof(ucm.MappingType));
var result = gMethod.Invoke(this, null);
Sign up to request clarification or add additional context in comments.

1 Comment

It's actually being called from inside itself, so the Invoke is gMethod.Invoke(this, null) ... but the principal is the same. Thanks!

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.