开发者

C# IL code to Add value to Dictionary

I have a dynamic method that takes a DataRecord and maps the data to an object of a certain type.

sourced from DynamicMethod_ILGenerator

The objects have a dictionary of type Dictionary<string,object> to hold any properties in the DataRecord that cannot be bound.

However I cannot seem to be able to work out how to add these to the Dictionary.

I know I have to call Emit(OpCodes.Callvirt, addMethod) on the dictionary but no matter what I try I cannot figure out how to get it to work.

public static Load CreateBuilder(IDataRecord dataRecord)
    {
        var dynamicBuilder = new DynamicBuilder<T>();

        var method = new DynamicMethod("DynamicCreate", typeof(T), new[] { typeof(IDataRecord) }, typeof(T), true);
        var generator = method.GetILGenerator();

        var result = generator.DeclareLocal(typeof(T));
        generator.Emit(O开发者_如何转开发pCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
        generator.Emit(OpCodes.Stloc, result);


        // Dictionary to store.
        var dictType = typeof (Dictionary<string, object>);
        var dict = typeof(T).GetProperty("CustomFields");
        var addMethod = dictType.GetMethod("Add");

        for (var i = 0; i < dataRecord.FieldCount; i++)
        {
            var propertyInfo = typeof(T).GetProperty(dataRecord.GetName(i));
            var endIfLabel = generator.DefineLabel();

            if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
            {
                var attributes = propertyInfo.GetCustomAttributes(typeof(DbIgnore), true) as DbIgnore[];
                if (attributes != null && attributes.Length > 0) continue;

                bool isNullable = false;
                if (propertyInfo.PropertyType.Name.ToLower().Contains("nullable"))
                    isNullable = true;

                Type _type = dataRecord.GetFieldType(i);

                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldc_I4, i);
                generator.Emit(OpCodes.Callvirt, IsDbNullMethod);
                generator.Emit(OpCodes.Brtrue, endIfLabel);

                generator.Emit(OpCodes.Ldloc, result);
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldc_I4, i);
                generator.Emit(OpCodes.Callvirt, GetValueMethod);

                if (isNullable)
                    generator.Emit(OpCodes.Unbox_Any, GetNullableType(_type));
                else
                    generator.Emit(OpCodes.Unbox_Any, _type);

                //generator.Emit(OpCodes.Unbox_Any, dataRecord.GetFieldType(i));
                generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());

                generator.MarkLabel(endIfLabel);
            } else {
                  // How-To Add to Dictionary
            }
        }

        generator.Emit(OpCodes.Ldloc, result);
        generator.Emit(OpCodes.Ret);

        return (Load)method.CreateDelegate(typeof(Load));
    }


Outside your loop, fetch the Dictionary reference from your object into a local variable.

Inside the loop, push the local variable holding the dictionary variable, key, and value onto the stack, then emit callvirt to the Add(TKey, TValue) method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜