在运行时从运行时生成的另一个对象生成的调用类

时间:2015-12-17 21:57:15

标签: c# .net nhibernate runtime codedom

我正在开发一个C#项目,我在运行时从给定服务器上的给定数据库动态构建ORM解决方案的映射,即nHibernate。现在,我专注于使基本对象和关系映射类运行时生成工作。我可以通过CodeDOM在运行时在内存中编译以下类:

namespace DataDictionary.Domain
{
    public class Entity
    {
        public virtual int EntityID { get; set; }
        public virtual string EntityName { get; set; }
        public virtual DateTime EntityFoundationDate { get; set; }
    }
}

但是,当我尝试使用CodeDOM编译以下类的等价物时:

using System;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
...
namespace DataDictionary.Domain
{
    public class EntityMap : ClassMapping<Entity>
    {
        public EntityMap()
        {
            this.Table(Entity);
            this.ID(p => p.EntityID);
            this.Property(p => p.EntityName);
            this.Property(p => p.EntityFoundationDate);
        }
    }
}

编译时出现以下错误:error CS0246: The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?)

我的问题是为什么 Entity,尽管EntityMap没有看到在运行时编译中被放置在正确的命名空间中。

此外,作为参考,这里是我用来创建EntityMap的方法:

    public static object CreateNewObject(ref object table, AssemblyName domain, params FieldInfo[] columns)
    {
        if (columns == null || columns.Length == 0)
            return null;

        string tableName = table.GetType().Name;

        //check to see if a class exists which both matches the name of `table`
        //and whose full name contains the correct assembly to be used.
        var namespaceCheck = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                              from type in assembly.GetTypes()
                              where type.Name == tableName && Utilities.GetFriendlyAssemblyName(type.FullName).Equals("DataDictionary.Domain")
                              select type).FirstOrDefault();

        if (namespaceCheck == null) 
            throw new InvalidOperationException("Valid type not found.");

        StringBuilder builder = new StringBuilder("using NHibernate; using NHibernate.Cfg; using NHibernate.Cfg.MappingSchema; using NHibernate.Dialect; using NHibernate.Mapping.ByCode; using NHibernate.Mapping.ByCode.Conformist; ");

        builder.Append("namespace DataDictionary.Domain{");

        builder.AppendFormat("public class {0}Map : ClassMapping<{0}> {{", tableName);
        builder.AppendFormat("public {0}Map(){{\n",tableName);
        builder.AppendFormat("this.Table({0});", tableName);

        //find the ID column.
        var list = columns.Where(x => x.Name.ToUpper().Contains("ID"));
        builder.AppendFormat("this.ID(p => p.{0})", Utilities.cleanFieldName(list.ElementAt(0).Name));
        columns = columns.Where(x => !x.Equals(list.ElementAt(0))).ToArray();

        //map the properties.
        foreach (FieldInfo column in columns)
        {
            builder.AppendFormat("this.Property(p => p.{0});", Utilities.cleanFieldName(column.Name));
        }

        //close all open brackets.
        builder.Append("}}}");

        //send code to helper class for runtime compilation via CodeDOM.
        return CodeDOM_Helpers.Execute(builder.ToString(), tableName,domain.Name);
    }

1 个答案:

答案 0 :(得分:0)

我想你或许打算这样:

   public EntityMap()
    {
        this.Table(Entity);
        this.ID(p => p.Entity);
        this.Property(p => p.EntityName);
        this.Property(p => p.EntityFoundationDate);
    }

读作:

   public EntityMap()
    {
        this.Table(Entity);
        this.ID(p => p.EntityID);  // since the type of p does not have an "Entity" property
        this.Property(p => p.EntityName);
        this.Property(p => p.EntityFoundationDate);
    }