tags:

views:

51

answers:

1

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?

+1  A: 

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.

Jon Skeet
Thanks. I try this variant private DataContext GetDataContext() { Type type = GetContainerType(); Type paoloGenericClassType = typeof(ObjectContextManager<>).MakeGenericType(type); return paoloGenericClassType.InvokeMember("GetObjectContext", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, new object[] { }) as DataContext; }But I have the same error.
Alex
You can see more information http://clip2net.com/page/m0/6878830
Alex
@Alex: Well your type initializer is throwing an exception, apparently. I suggest you put a break point on it and see what's going on in the debugger.
Jon Skeet