Using Reflection to create an interface layout that is unknown during build

To complete my testing, it is important that my test assembly does not load a shadow copy of the dependent assembly.

Assembly T is a test program that downloads and tests assembly A. Assembly A depends on the interfaces defined in B.

For testing purposes, I have to replace some of the static elements in A without having them available at build time.

Here is some pseudo code that illustrates the dilemma in which I:

        A_assembly = Assembly.LoadFrom("A.dll");
        A_type = A_assembly.GetType("TheSingleton.Master", true);
        MethodInfo Master_Init_Info = type.GetMethod("Init", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
        //before init is called some things need to be replaced
        FieldInfo semiknown = A_type.GetField("needsmocking", BindingFlags.Public | BindingFlags.Static));
        ??? mock_semiknown = MockRepository.GenerateMock<???>();
        semiknown.SetValue((???)mock_semiknown, mock_semiknown);
        //testing makes only sense if that static is replaced.
        Master_Init_Info.Invoke(null, null);
  • I can access the type through semiknown.FieldType, but what good does what I do? Can I use this information to create a layout and replace the static element with it?
  • Suppose I get a type and can replace a static member - how can I build my expectations in a layout?
+3
1

var method = typeof(MockRepository).GetMethod("GenerateMock").MakeGenericMethod(semiknown.FieldType);
var mock_semiknown = method.Invoke(null, null);
+1

All Articles