Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void CreateModelFactory()
- {
- var modelType = typeof(TImportModel);
- var constructor = typeof(TImportModel).GetConstructor(new[] { typeof(int) });
- if (constructor == null)
- {
- throw new MissingMethodException($"There is no constructor with the signature '(int)' for type '{modelType}'");
- }
- DynamicMethod dynamic = new DynamicMethod(name: string.Empty,
- returnType: modelType,
- parameterTypes: new[] { typeof(int) },
- owner: modelType);
- ILGenerator il = dynamic.GetILGenerator();
- il.DeclareLocal(modelType);
- il.Emit(OpCodes.Ldarg_0);
- il.Emit(OpCodes.Newobj, constructor);
- il.Emit(OpCodes.Ret);
- _modelFactory = (Func<int, TImportModel>)dynamic.CreateDelegate(typeof(Func<int, TImportModel>));
- }
Advertisement
Add Comment
Please, Sign In to add comment