首先感谢任何愿意看看我的问题的人。我对Nancyfx很新,我遇到了尝试将JSON有效负载绑定到动态创建的类的问题。 我按照这篇文章中的代码动态创建了这个类 - Dynamically create a class in C#
这是我创建动态类的代码块,我承认它基本上是danijels
给出的代码的剪切和粘贴 public static Type CompileResultType(List<Metadata> metadata)
{
TypeBuilder tb = GetTypeBuilder();
ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
foreach (var field in metadata)
{
if(field.datatype == "String")
{
Type datatype = Type.GetType("System.String");
CreateProperty(tb, field.columnname, datatype);
}
if (field.datatype == "int")
{
Type datatype = Type.GetType("System.Int32");
CreateProperty(tb, field.columnname, datatype);
}
if(field.datatype == "datetime")
{
Type datatype = Type.GetType("System.DateTime");
CreateProperty(tb, field.columnname, datatype);
}
}
Type objectType = tb.CreateType();
return objectType;
}
private static TypeBuilder GetTypeBuilder()
{
var typeSignature = "MyDynamicType";
var an = new AssemblyName(typeSignature);
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
TypeBuilder tb;
tb = moduleBuilder.DefineType(typeSignature, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout, null);
return tb;
}
private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType)
{
FieldBuilder fieldBuilder = tb.DefineField(propertyName, propertyType, FieldAttributes.Public); //changed field attributes from private to public
PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, System.Reflection.PropertyAttributes.None, propertyType, null);
MethodBuilder getPropMethdBldr = tb.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, Type.EmptyTypes); //changed get_ to get
ILGenerator getIl = getPropMethdBldr.GetILGenerator();
getIl.Emit(OpCodes.Ldarg_0);
getIl.Emit(OpCodes.Ldfld, fieldBuilder);
getIl.Emit(OpCodes.Ret);
MethodBuilder setPropMthdBldr = tb.DefineMethod("set_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null, new[] { propertyType }); //changed set_to set
ILGenerator setIl = setPropMthdBldr.GetILGenerator();
Label modifyProperty = setIl.DefineLabel();
Label exitSet = setIl.DefineLabel();
setIl.MarkLabel(modifyProperty);
setIl.Emit(OpCodes.Ldarg_0);
setIl.Emit(OpCodes.Ldarg_1);
setIl.Emit(OpCodes.Stfld, fieldBuilder);
setIl.Emit(OpCodes.Nop);
setIl.MarkLabel(exitSet);
setIl.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getPropMethdBldr);
propertyBuilder.SetGetMethod(setPropMthdBldr);
}
//List<Metadata> metadata is a Dictionary
然后通过执行此操作绑定它
var myType = CompileResultType(metadata);
var myObject = Activator.CreateInstance(myType);
var b = this.BindTo(myObject);
我不能为Nancy提供正常的模型类,因为我只有太多的模型才能收集数据。
//usual model class
public Class PayloadModel
{
public string firstName {get; set;}
public string lastName {get; set;}
}
我必须动态创建类的原因是因为我收到的JSON有效负载从1个字段变为30个。另一个原因是我希望尽可能保持灵活性。通过上述课程意味着我被绑在那个班级的任何东西上。任何建议或指示都会有很大帮助!
答案 0 :(得分:2)
听起来你最好使用动态类型:
https://gist.github.com/thecodejunkie/5521941
TheCodeJunkie有一个Dynamic的自定义模型绑定器。您应该可以使用它来执行:
dynamic model = this.Bind<DynamicDictionary>();