2013-04-04 63 views
2

我有一個可能或可能沒有將其ComVisible屬性設置爲true的DLL。我不確定它是如何構建的,或者具有什麼屬性?我只知道它是一個.Net DLL。簡而言之,我怎麼知道它是否可見?如何判斷一個DLL是否是ComVisible?

對不起,如果這是重複的。我所有關於此返回的搜索結果都顯示如何使成爲一個DLL ComVisible。我知道該怎麼做。

+0

只要運行Tlbexp.exe。當它找不到任何類型的[ComVisible]時,它會發出抱怨。 – 2013-04-04 21:30:35

回答

3

你可以使用反射檢查裝配的ComVisibleAttribute

private static bool IsComVisible(string assemblyPath) 
{ 
    var assembly = Assembly.LoadFile(assemblyPath); 

    var attributes = assembly.GetCustomAttributes(typeof(ComVisibleAttribute), false); 

    if (attributes.Length > 0) 
    { 
    return ((ComVisibleAttribute)attributes[0]).Value; 
    } 

    return false; 
} 
0

像這樣的東西?

Assembly asm = Assembly.GetExecutingAssembly(); //Assembly.LoadFile, Assembly.Load 

bool comVisible = asm.GetCustomAttributes() 
        .OfType<ComVisibleAttribute>() 
        .First() 
        .Value; 
+0

出現錯誤:'方法'GetCustomAttributes'沒有重載需要0個參數' – user2023861 2013-04-05 13:02:13

相關問題