2011-04-30 83 views
0

嗨,我有一个组装问题。装配问题

我有一个程序,谁创建一个类。在那个类中,我使用了我的不同DLL中的一些其他类。在那个DLL类的方法中,我必须使用Assembly类,但是我会参考程序Assembly,并且我不知道该怎么做。

如果(在DLL)我的用户Assembly.GetExecutingAssembly(),或CallingAssembly我总是得到DLL组件:/

的GetEntryAssembly总是做StackOverflow的例外。

在DLL中,我不能与程序有任何连接。

编辑:

DLL

public sealed class MapperSet<T> : MapperSetBase<T>, IMapperSet<T> 
    { 
    public MapperSet() 
    { 
     _Mapper = GetSpecificMapper(); 

     //if (_mapper == null) 
     // throw new NullReferenceException(); 
    } 
    // some methods, not use yet 
    } 

public abstract class MapperSetBase<T> : IQuerySet 
{ 
     protected virtual IMapper GetSpecificMapper() 
    { 
     Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes(); 
     IMapper mapper = null; 

     foreach (Type type in assemblyTypes) 
     { 
     if (type.GetInterfaces().Any(i => i == typeof(IMapper))) 
     { 
      FieldInfo[] fields = type.GetFields(); 

      foreach (FieldInfo field in fields) 
      { 
      if (field.FieldType.GetGenericArguments().Count() > 0) 
      { 
       Type mapperFieldType = field.FieldType.GetGenericArguments()[0]; 

       if (mapperFieldType == typeof(T)) 
       { 
       mapper = (IMapper)Activator.CreateInstance(type); 
       } 
      } 
      } 
     } 
     } 
} 

程序代码

public class DB : IMapper 
    { 
    public string ConnectionString 
    { 
     get { return "TestConnectionString"; } 
    } 

    public DBTypes DbType 
    { 
     get { return DBTypes.MsSql; } 
    } 

    public MapperSet<Person> Persons = new MapperSet<Person>(); 
    public List<int> l1 = new List<int>(); 
    } 

在MapperSet有一些方法。当我调用其中的一个时,我必须从IMapper类获得ConnectionString和DbType。

+2

你是否100%确定它正在调用产生堆栈溢出异常的'GetEntryAssembly'?你可以显示你称之为的代码吗? – 2011-04-30 12:08:44

+0

删除[assembly]标签;它用于汇编语言问题。 – 2011-04-30 17:21:26

回答

-1

如何添加从卫星集的引用到主要部件,那么你可以使用各类直接

+1

-1:你的意思是他需要从他的主程序集到另一个程序集,从另一个程序集回到主程序集?对不起,不可能。 – 2011-04-30 12:08:00

+0

否,如果M是您的主要组件,并且S是您的卫星程序集,则可以添加从S到M的引用,并在M中动态加载S,然后可以枚举提供特定接口的所有类型,创建对象等上。 S可以访问M中的所有类型 – user287107 2011-04-30 13:29:50

0

作为一种变通方法,你可以有你的主要组件传递正确大会所引用的DLL,但GetEntryAssembly ()也应该工作。

我猜想Lasee是对的,你的代码必须有一些其他的问题。向我们展示代码和堆栈跟踪。

编辑:

在DLL从 “入口” 装配实例的类型。其中之一就是你的数据库类,它有参数初始化期间构建的MapperSet。这反过来调用你的MapperSet()构造函数,这会导致你的DLL加载“Entry”程序集并永久递归地实例化它的类型,因此你会得到堆栈溢出异常..

如何在构造函数中设置映射器:

public class DB : IMapper 
{ 
    public MapperSet<Person> Persons = null; 
    public DB() 
    { 
     Persons = new MapperSet<Person>(this); 
    }  
    (...) 
} 

DLL:

public sealed class MapperSet<T> : MapperSetBase<T>, IMapperSet<T> 
{ 
    public MapperSet(IMapper mapper) 
    { 
     _Mapper = mapper; 
    } 
} 

您可能还需要考虑使用依赖注入。