EmitCall 引发“操作可能会破坏运行时的稳定性”在银光中

发布于 2024-09-27 17:00:03 字数 3890 浏览 4 评论 0原文

我的 IL 语言是 n00b。在我的任务中,我必须创建从 System.ServiceModel.DomainServices.Client.Entity 继承的动态类型。当我添加额外的代码来设置属性方法时,我收到“操作可能会破坏运行时的稳定性”。请帮我解决这个问题。

来自 Reflector 的代码:

L_0000: nop 
L_0001: ldarg.0 
L_0002: ldstr "Field"
L_0007: ldarg.1 
L_0008: call instance void [System.ServiceModel.DomainServices.Client]System.ServiceModel.DomainServices.Client.Entity::ValidateProperty(string, object)
L_000d: nop 
L_000e: ldarg.0 
L_000f: ldarg.1 
L_0010: stfld string SEC.Client.Views.Test::_field
L_0015: ldarg.0 
L_0016: ldstr "Field"
L_001b: call instance void [System.ServiceModel.DomainServices.Client]System.ServiceModel.DomainServices.Client.Entity::RaisePropertyChanged(string)
L_0020: nop 
L_0021: ret 

我的代码:

  // Generate a private field
        FieldBuilder field = typeBuilder.DefineField("_" + propertyName, type,
                                                     FieldAttributes.Private);
        // Generate a public property
        PropertyBuilder property =
            typeBuilder.DefineProperty(propertyName,
                                       PropertyAttributes.None,
                                       type,
                                       new Type[] { type });

        if(attributes != null)
        {
            foreach(var attribute in attributes)
            {
                property.SetCustomAttribute(attribute);
            }
        }

        // The property set and property get methods require a special set of attributes:

        MethodAttributes GetSetAttr =
            MethodAttributes.Public |
            MethodAttributes.HideBySig;

        // Define the "get" accessor method for current private field.
        MethodBuilder currGetPropMthdBldr =
            typeBuilder.DefineMethod("get_value",
                                     GetSetAttr,
                                     type,
                                     Type.EmptyTypes);

        // Intermediate Language stuff...
        ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
        currGetIL.Emit(OpCodes.Ldarg_0);
        currGetIL.Emit(OpCodes.Ldfld, field);
        currGetIL.Emit(OpCodes.Ret);

        // Define the "set" accessor method for current private field.
        MethodBuilder currSetPropMthdBldr =
            typeBuilder.DefineMethod("set_value",
                                     GetSetAttr,
                                     null,
                                     new Type[] { type });

        ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
        currSetIL.Emit(OpCodes.Nop);
        currSetIL.Emit(OpCodes.Ldarg_0);
        currSetIL.Emit(OpCodes.Ldstr, propertyName);
        currSetIL.Emit(OpCodes.Ldarg_1);
        var m = typeof(Entity).GetMethod("ValidateProperty",
                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                                         null, new [] {typeof(string), typeof(object)}, null);
        currSetIL.EmitCall(OpCodes.Call, m, null);
        currSetIL.Emit(OpCodes.Nop);
        currSetIL.Emit(OpCodes.Ldarg_0);
        currSetIL.Emit(OpCodes.Ldarg_1);
        currSetIL.Emit(OpCodes.Stfld, field);
        currSetIL.Emit(OpCodes.Ldarg_0);
        currSetIL.Emit(OpCodes.Ldstr, propertyName);
        m = typeof(Entity).GetMethod("RaisePropertyChanged",
                                        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        currSetIL.EmitCall(OpCodes.Call, m, null);
        currSetIL.Emit(OpCodes.Nop);
        currSetIL.Emit(OpCodes.Ret);

        // Last, we must map the two methods created above to our PropertyBuilder to
        // their corresponding behaviors, "get" and "set" respectively.
        property.SetGetMethod(currGetPropMthdBldr);
        property.SetSetMethod(currSetPropMthdBldr);

I'm n00b in IL language. In my task i have to create dynamic types inherited from System.ServiceModel.DomainServices.Client.Entity. When i am adding additional code to set method of property, i receive "operation could destabilize the runtime". Help me, please, resolve that problem.

Code from Reflector:

L_0000: nop 
L_0001: ldarg.0 
L_0002: ldstr "Field"
L_0007: ldarg.1 
L_0008: call instance void [System.ServiceModel.DomainServices.Client]System.ServiceModel.DomainServices.Client.Entity::ValidateProperty(string, object)
L_000d: nop 
L_000e: ldarg.0 
L_000f: ldarg.1 
L_0010: stfld string SEC.Client.Views.Test::_field
L_0015: ldarg.0 
L_0016: ldstr "Field"
L_001b: call instance void [System.ServiceModel.DomainServices.Client]System.ServiceModel.DomainServices.Client.Entity::RaisePropertyChanged(string)
L_0020: nop 
L_0021: ret 

My code:

  // Generate a private field
        FieldBuilder field = typeBuilder.DefineField("_" + propertyName, type,
                                                     FieldAttributes.Private);
        // Generate a public property
        PropertyBuilder property =
            typeBuilder.DefineProperty(propertyName,
                                       PropertyAttributes.None,
                                       type,
                                       new Type[] { type });

        if(attributes != null)
        {
            foreach(var attribute in attributes)
            {
                property.SetCustomAttribute(attribute);
            }
        }

        // The property set and property get methods require a special set of attributes:

        MethodAttributes GetSetAttr =
            MethodAttributes.Public |
            MethodAttributes.HideBySig;

        // Define the "get" accessor method for current private field.
        MethodBuilder currGetPropMthdBldr =
            typeBuilder.DefineMethod("get_value",
                                     GetSetAttr,
                                     type,
                                     Type.EmptyTypes);

        // Intermediate Language stuff...
        ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
        currGetIL.Emit(OpCodes.Ldarg_0);
        currGetIL.Emit(OpCodes.Ldfld, field);
        currGetIL.Emit(OpCodes.Ret);

        // Define the "set" accessor method for current private field.
        MethodBuilder currSetPropMthdBldr =
            typeBuilder.DefineMethod("set_value",
                                     GetSetAttr,
                                     null,
                                     new Type[] { type });

        ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
        currSetIL.Emit(OpCodes.Nop);
        currSetIL.Emit(OpCodes.Ldarg_0);
        currSetIL.Emit(OpCodes.Ldstr, propertyName);
        currSetIL.Emit(OpCodes.Ldarg_1);
        var m = typeof(Entity).GetMethod("ValidateProperty",
                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                                         null, new [] {typeof(string), typeof(object)}, null);
        currSetIL.EmitCall(OpCodes.Call, m, null);
        currSetIL.Emit(OpCodes.Nop);
        currSetIL.Emit(OpCodes.Ldarg_0);
        currSetIL.Emit(OpCodes.Ldarg_1);
        currSetIL.Emit(OpCodes.Stfld, field);
        currSetIL.Emit(OpCodes.Ldarg_0);
        currSetIL.Emit(OpCodes.Ldstr, propertyName);
        m = typeof(Entity).GetMethod("RaisePropertyChanged",
                                        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        currSetIL.EmitCall(OpCodes.Call, m, null);
        currSetIL.Emit(OpCodes.Nop);
        currSetIL.Emit(OpCodes.Ret);

        // Last, we must map the two methods created above to our PropertyBuilder to
        // their corresponding behaviors, "get" and "set" respectively.
        property.SetGetMethod(currGetPropMthdBldr);
        property.SetSetMethod(currSetPropMthdBldr);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咽泪装欢 2024-10-04 17:00:03

是的,如果您在之前添加 box 指令:就可以了

currSetIL.Emit(OpCodes.Box, type);  

currSetIL.EmitCall(OpCodes.Call, m, null);

Yes, it works if you add the box instruction:

currSetIL.Emit(OpCodes.Box, type);  

before:

currSetIL.EmitCall(OpCodes.Call, m, null);
橘虞初梦 2024-10-04 17:00:03

这将获取字符串和值/类型的列表,并从中获取 make 和对象。

using System;


public class CreateObject
{
    public static object CreatePropertyObject(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> objData)
    {
        System.Collections.Generic.Dictionary<string, Type> list = new System.Collections.Generic.Dictionary<string, Type>();
        foreach (var o in objData)
        {
            list.Add(o.Key, o.Value.GetType());
        }

        Type newType = BuildPropertyObject(list);
        object newObject = NewPropertyObject(newType, objData);
        return newObject;
    }

    public static object NewPropertyObject(Type newType, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> objData)
    {
        var newObject = Activator.CreateInstance(newType);
        foreach (var item in objData)
        {
            // Set the value on the new object
            newObject.GetType().GetProperty(item.Key).SetValue(newObject, item.Value, null);
        }
        return newObject;
    }

    public static Type BuildPropertyObject(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, Type>> obj)
    {
        string nameOfDLL = "magic.dll";
        string nameOfAssembly = "magic_Assembly";
        string nameOfModule = "magic_Module";
        string nameOfType = "magic_Type";

        System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName { Name = nameOfAssembly };
        System.Reflection.Emit.AssemblyBuilder assemblyBuilder = System.Threading.Thread.GetDomain().DefineDynamicAssembly(assemblyName, System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave);
        System.Reflection.Emit.ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(nameOfModule, nameOfDLL);
        System.Reflection.Emit.TypeBuilder typeBuilder = moduleBuilder.DefineType(nameOfType, System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Class);

        foreach (var prop in obj)
        {
            string Name = prop.Key;
            Type DataType = prop.Value;

            System.Reflection.Emit.FieldBuilder field = typeBuilder.DefineField("_" + Name, DataType, System.Reflection.FieldAttributes.Private);
            System.Reflection.Emit.PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(Name, System.Reflection.PropertyAttributes.SpecialName, DataType, null);
            System.Reflection.MethodAttributes methodAttributes = System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.SpecialName;
            System.Reflection.Emit.MethodBuilder methodBuilderGetter = typeBuilder.DefineMethod("get_" + Name, methodAttributes, DataType, new Type[] { });
            System.Reflection.Emit.MethodBuilder methodBuilderSetter = typeBuilder.DefineMethod("set_" + Name, methodAttributes, typeof(void), new Type[] { DataType });

            System.Reflection.Emit.ILGenerator ilGeneratorGetter = methodBuilderGetter.GetILGenerator();
            ilGeneratorGetter.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
            ilGeneratorGetter.Emit(System.Reflection.Emit.OpCodes.Ldfld, field);
            ilGeneratorGetter.Emit(System.Reflection.Emit.OpCodes.Ret);


            System.Reflection.Emit.ILGenerator ilGeneratorSetter = methodBuilderSetter.GetILGenerator();
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Stfld, field);
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Ret);

            propertyBuilder.SetGetMethod(methodBuilderGetter);
            propertyBuilder.SetSetMethod(methodBuilderSetter);
        }

        // Yes! you must do this, it should not be needed but it is!
        Type dynamicType = typeBuilder.CreateType();

        // Save to file
        assemblyBuilder.Save(nameOfDLL);
        return dynamicType;
    }
}

This will take a list of strings and values/Types and make and object from it.

using System;


public class CreateObject
{
    public static object CreatePropertyObject(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> objData)
    {
        System.Collections.Generic.Dictionary<string, Type> list = new System.Collections.Generic.Dictionary<string, Type>();
        foreach (var o in objData)
        {
            list.Add(o.Key, o.Value.GetType());
        }

        Type newType = BuildPropertyObject(list);
        object newObject = NewPropertyObject(newType, objData);
        return newObject;
    }

    public static object NewPropertyObject(Type newType, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> objData)
    {
        var newObject = Activator.CreateInstance(newType);
        foreach (var item in objData)
        {
            // Set the value on the new object
            newObject.GetType().GetProperty(item.Key).SetValue(newObject, item.Value, null);
        }
        return newObject;
    }

    public static Type BuildPropertyObject(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, Type>> obj)
    {
        string nameOfDLL = "magic.dll";
        string nameOfAssembly = "magic_Assembly";
        string nameOfModule = "magic_Module";
        string nameOfType = "magic_Type";

        System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName { Name = nameOfAssembly };
        System.Reflection.Emit.AssemblyBuilder assemblyBuilder = System.Threading.Thread.GetDomain().DefineDynamicAssembly(assemblyName, System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave);
        System.Reflection.Emit.ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(nameOfModule, nameOfDLL);
        System.Reflection.Emit.TypeBuilder typeBuilder = moduleBuilder.DefineType(nameOfType, System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Class);

        foreach (var prop in obj)
        {
            string Name = prop.Key;
            Type DataType = prop.Value;

            System.Reflection.Emit.FieldBuilder field = typeBuilder.DefineField("_" + Name, DataType, System.Reflection.FieldAttributes.Private);
            System.Reflection.Emit.PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(Name, System.Reflection.PropertyAttributes.SpecialName, DataType, null);
            System.Reflection.MethodAttributes methodAttributes = System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.SpecialName;
            System.Reflection.Emit.MethodBuilder methodBuilderGetter = typeBuilder.DefineMethod("get_" + Name, methodAttributes, DataType, new Type[] { });
            System.Reflection.Emit.MethodBuilder methodBuilderSetter = typeBuilder.DefineMethod("set_" + Name, methodAttributes, typeof(void), new Type[] { DataType });

            System.Reflection.Emit.ILGenerator ilGeneratorGetter = methodBuilderGetter.GetILGenerator();
            ilGeneratorGetter.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
            ilGeneratorGetter.Emit(System.Reflection.Emit.OpCodes.Ldfld, field);
            ilGeneratorGetter.Emit(System.Reflection.Emit.OpCodes.Ret);


            System.Reflection.Emit.ILGenerator ilGeneratorSetter = methodBuilderSetter.GetILGenerator();
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Stfld, field);
            ilGeneratorSetter.Emit(System.Reflection.Emit.OpCodes.Ret);

            propertyBuilder.SetGetMethod(methodBuilderGetter);
            propertyBuilder.SetSetMethod(methodBuilderSetter);
        }

        // Yes! you must do this, it should not be needed but it is!
        Type dynamicType = typeBuilder.CreateType();

        // Save to file
        assemblyBuilder.Save(nameOfDLL);
        return dynamicType;
    }
}
下壹個目標 2024-10-04 17:00:03

添加盒子指令后,一切正常。

After adding box instruction, all is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文