2011-04-30 84 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; 
    } 
} 

您可能還需要考慮使用依賴注入。