Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Codedom正确设置自定义构造函数_C#_Constructor_Codedom - Fatal编程技术网

C# 如何使用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() {

我正在尝试让Codedom生成以下代码:

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()”构造函数有三个问题:

  • Codedom不会在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;
    }