可以從動態構造的裝配中調用駐留在「普通」裝配上的方法嗎?從動態裝配執行持久裝配的方法
例如,組件B被動態地從組件A和組件構造(經由發射)B需要當執行MissingMethodExcetion地圖的方法來調用在組件A
public interface IMapper
{
void Map();
}
public void CreateDynamic() {
AppDomain app = AppDomain.CurrentDomain;
AssemblyName name = new AssemblyName("assemblyB");
AssemblyBuilder assembly = app
.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
ModuleBuilder module = assembly
.DefineDynamicModule(assembly.GetName().Name, "b.dll");
TypeBuilder type = module.DefineType("MyType",
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AutoLayout, null, new[] {typeof (IMapper)});
MethodBuilder method = type
.DefineMethod("Map",
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.Virtual);
ILGenerator il = method.GetILGenerator();
Func<int, TimeSpan> func = i => TimeSpan.FromSeconds(i);
il.Emit(OpCodes.Ldc_I4_S, 10);
il.Emit(OpCodes.Callvirt, func.Method);
il.Emit(OpCodes.Ret);
Type t = type.CreateType();
IMapper mapper = (IMapper) Activator.CreateInstance(t);
mapper.Map();
}
定義的靜態方法被拋出,我現在不是這個原因。
提供一個碼樣本和/或檢查這http://stackoverflow.com/questions/11908156/call-static-method-with-reflection – 2014-10-10 17:08:39