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

本文关键字:运行时 对象 另一个 调用 | 更新日期: 2023-09-27 18:27:18

我正在进行一个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);
    }

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

我想你可能打算这样做:

   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);
    }