Alex
Alex

Reputation: 1

Can't invoke dynamic method from generic class

I'm trying to work with two contexts in a generic repository and I should invoke the static method GetObjectContext() with dynamic type like ObjectContextManager<DynamicType>.GetObjectContext().

private DataContext GetDataContext()
    {
        Type type = GetContainerType();
        Type paoloGenericClassType = typeof(ObjectContextManager<>).MakeGenericType(type);

        MethodInfo method = paoloGenericClassType.GetMethod("GetObjectContext", BindingFlags.Static);
        return method.Invoke(null, BindingFlags.Static, null, null, null) as DataContext;
    }

I'm try different variants, but it doesn't work. How can I do this?

Upvotes: 0

Views: 172

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500625

I suspect the problem is just with your binding flags. Try BindingFlags.Static | BindingFlags.Public, assuming it's a public method.

If that doesn't work, please tell us what actually happens, rather than just saying it doesn't work.

Upvotes: 1

Related Questions