C# 如何使用Codedom正确设置自定义构造函数
我正在尝试让Codedom生成以下代码:C# 如何使用Codedom正确设置自定义构造函数,c#,constructor,codedom,C#,Constructor,Codedom,我正在尝试让Codedom生成以下代码: public class NewContext : DbContext { public NewContext() : base("NewContext") { EntityToRegisterDictionary = new ConcurrentDictionary<string, Assembly>(); } static NewContext() {
public class NewContext : DbContext
{
public NewContext()
: base("NewContext")
{
EntityToRegisterDictionary = new ConcurrentDictionary<string, Assembly>();
}
static NewContext()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFEntityModelContext>());
}
}
没有任何问题。我在编写创建这两个构造函数的代码时遇到了问题。我创建了两个方法,每个构造函数一个方法。我的方法是这样的:
public static CodeConstructor BaseStringConstructor(string connectionStr)
{
CodeConstructor baseStringConstructor = new CodeConstructor();
baseStringConstructor.Attributes = MemberAttributes.Public;
baseStringConstructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("\"connectionStr\""));
CodeAssignStatement body = new CodeAssignStatement();
body.Left = new CodeFieldReferenceExpression(new CodeFieldReferenceExpression(), "EntityToRegisterDictionary");
body.Right = new CodeObjectCreateExpression("ConcurrentDictionary<string, Assembly>");
baseStringConstructor.Statements.Add(body);
return baseStringConstructor;
}
public static CodeConstructor StaticInitializerConstructor()
{
CodeConstructor constructor = new CodeConstructor();
constructor.Attributes = MemberAttributes.Static;
CodeObjectCreateExpression body = new CodeObjectCreateExpression("Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFEntityModelContext>");
constructor.Statements.Add(body);
return constructor;
}
我想这是因为这条线
body.Left = new CodeFieldReferenceExpression(new CodeFieldReferenceExpression(), "EntityToRegisterDictionary");
有人知道如何在没有经期的情况下制作吗
“static NewContext()”构造函数有三个问题:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFEntityModelContext>());
Database.SetInitializer(新的DropCreateDatabaseIfModelChanges());
不带new关键字并添加右括号
编辑:顺便说一下,EntityToRegisterDictionary是我在下面定义的:
public IDictionary<string, Assembly> EntityToRegisterDictionary { get; private set; }
public IDictionary EntityToRegisterDictionary{get;private set;}
我只是没有包括所有的上下文类
“public NewContext()”构造函数中的问题是前面有一个“.”
是的,您提供的是一个CodeFieldReferenceExpression
,没有实际的字段引用,所以它只是使用一个空字符串。您正在寻找的是,因为您正在访问同一个类上的成员
body.Left = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "EntityToRegisterDictionary");
这将使用This
关键字生成分配,如下所示:
this.EntityToRegisterDictionary = new ConcurrentDictionary<string, Assembly>();
“public NewContext()”构造函数中的问题是前面有一个“.”
是的,您提供的是一个CodeFieldReferenceExpression
,没有实际的字段引用,所以它只是使用一个空字符串。您正在寻找的是,因为您正在访问同一个类上的成员
body.Left = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "EntityToRegisterDictionary");
这将使用This
关键字生成分配,如下所示:
this.EntityToRegisterDictionary = new ConcurrentDictionary<string, Assembly>();
this.EntityToRegisterDictionary = new ConcurrentDictionary<string, Assembly>();
var method = new CodeMethodReferenceExpression(new CodeTypeReferenceExpression("Database"), "SetInitializer");
var body = new CodeMethodInvokeExpression(method, new CodeObjectCreateExpression("DropCreateDatabaseIfModelChanges<EFEntityModelContext>"));
constructor.Statements.Add(body);
CodeAssignStatement body = new CodeAssignStatement();
body.Left = new CodeFieldReferenceExpression(null, "Propiedad ");
body.Right = new CodeFieldReferenceExpression(null, "parametro");
------------------------------- OUTPUT--------------------------------
private Metodo(string parametro) {
Propiedad = parametro;
}