为什么我无法在C#中使用IL生成来创建对象的实例?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了为什么我无法在C#中使用IL生成来创建对象的实例?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2787字,纯文字阅读大概需要4分钟。
内容图文
![为什么我无法在C#中使用IL生成来创建对象的实例?](/upload/InfoBanner/zyjiaocheng/684/5a1cf15f881b4595b5d1805ab0c26222.jpg)
我有以下课程:
private sealed class Person
{
public string Name { get; }
public int Age { get; }
public Person(string name)
{
Name = name;
}
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
以及以下用于动态创建ConstructorInfo实例的方法:
public static Func<object[], T> GetBuilder<T>(ConstructorInfo constructor)
{
var type = constructor.ReflectedType;
var ctorParams = constructor.GetParameters();
var dynamicMethod = new DynamicMethod("Create_" + constructor.Name, type, new[] { typeof(object[]) }, type, true);
var ilGen = dynamicMethod.GetILGenerator();
/*
* Cast each argument of the input object array to the appropriate type
* The order of objects should match the order set by the Ctor
* It is also assumed the length of object array args is same length as Ctor args.
* Exceptions for the delegate that mean the above weren't satisfied:
* InvalidCastException, IndexOutOfRangeException
*/
for (var i = 0; i < ctorParams.Length; i++)
{
// Push Object array
ilGen.Emit(OpCodes.Ldarg_0);
// Push the index to access
ilGen.Emit(OpCodes.Ldc_I4, i);
// Push the element at the previously loaded index
ilGen.Emit(OpCodes.Ldelem_Ref);
// Cast the object to the appropriate Ctor Parameter Type
var paramType = ctorParams[i].ParameterType;
ilGen.Emit(paramType.IsValueType ? OpCodes.Box : OpCodes.Castclass, paramType);
}
// Call the Ctor, all values on the stack are passed to the Ctor
ilGen.Emit(OpCodes.Newobj, constructor);
// Return the new object
ilGen.Emit(OpCodes.Ret);
// Create delegate from our IL, cast and return
return (Func<object[], T>)dynamicMethod.CreateDelegate(typeof(Func<object[], T>));
}
然后,我使用该方法为每个构造函数创建该类的两个实例:
var ctorOne = typeof(Person).GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
var instanceBuilderOne = GetBuilder<Person>(publicCtor[0]);
var instanceOne = instanceBuilderOne(new object[] { "Foo"});
instanceOne.Name.Dump(); // is "Foo"
var ctorTwo = typeof(Person).GetConstructors(BindingFlags.Public | BindingFlags.Instance)[1];
var instanceBuilderTwo = GetBuilder<Person>(publicCtor[1]);
var instanceTwo = instanceBuilderTwo(new object[] { "Bar", 1});
instanceTwo.Name.Dump(); // is "Bar"
instanceTwo.Age.Dump(); // is 43603896
但是对于instanceTwo而不是1我得到43603896.
在相关构造函数中命中断点确实确实显示43603896被传递给实例,但我不知道为什么!
解决方法:
首先,这里的OpCodes.Box显然是错误的,因为您想将int从对象中取消装箱,而不是将其装箱.
现在,OpCodes.Unbox的作用是将值取消装箱并将对未装箱值的引用推入堆栈.该参考是您看到的而不是“ 1”.如果要使用OpCodes.Unbox,正确的方法是:
if (paramType.IsValueType) {
ilGen.Emit(OpCodes.Unbox, paramType);
ilGen.Emit(OpCodes.Ldobj, paramType);
}
else {
ilGen.Emit(OpCodes.Castclass, paramType);
}
但是使用OpCodes.Unbox_Any更容易,基本上可以做到这一点,但只需要一行:
ilGen.Emit(OpCodes.Unbox_Any, paramType);
内容总结
以上是互联网集市为您收集整理的为什么我无法在C#中使用IL生成来创建对象的实例?全部内容,希望文章能够帮你解决为什么我无法在C#中使用IL生成来创建对象的实例?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。