我有一个动态方法,它采用 aDataRecord
并将数据映射到某种类型的对象。
这些对象有一个类型的字典Dictionary<string,object>
来保存 DataRecord 中无法绑定的任何属性。
但是我似乎无法弄清楚如何将这些添加到字典中。
我知道我必须Emit(OpCodes.Callvirt, addMethod)
查字典,但无论我尝试什么,我都无法弄清楚如何让它发挥作用。
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(OpCodes.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));
}