2011-09-30 111 views
1

我想從我的自定義屬性添加System.Activites.Presentation中的類。我試圖用emit(TypeBuilder, ModuleBuilder, AssemblyBuilder)來做到這一點。是否可以通過向其添加屬性來更改現有類型?或者如何告訴TypeBuilder,以便它使用現有的數據類型?或從一個給定的類型繼承? 謝謝。將自定義屬性添加到系統類

+0

你在說什麼課?他們是否被標記爲「密封」?如果沒有,您可以創建自己的自定義類,從原始派生並從名稱空間Windows.System.Activities添加自定義屬性 –

+0

類。我想添加我的自定義屬性DisplayName(string) – ShurikEv

回答

2

您不能將屬性添加到System類,但是,如果它們未被標記爲Sealed,您可以創建自定義類,並從原始派生並添加自定義屬性。

您的所有代碼都必須調用派生類,該派生類除了添加的屬性外與原始類相同。

[MyAttribute(DisplayName="Name shown")] 
public class MyActivity: System.Activities.Activity 
{ 
} 
/// <summary> 
/// Custom attribute definition 
/// </summary> 
[AttributeUsage(AttributeTargets.Class)] 
public sealed class MyAttribute : System.Attribute 
{ 


    /// <summary> 
    /// Defines the attribute 
    /// </summary> 
     public string DisplayName { get; set; } 
    /// <summary> 
    /// Allow access to the attribute 
    /// </summary> 
    /// <param name="prop"></param> 
    /// <returns></returns> 
     public static string GetDisplayName(System.Reflection.MemberInfo prop) 
    { 
     string field = null; 
     object[] attr = prop.GetCustomAttributes(false); 
     foreach (object a in attr) 
     { 
      MyAttribute additional = a as MyAttribute; 
      if (additional != null) 
      { 
       field = additional.DisplayName; 
      } 
     } 
     return field; 
    } 


} 
相關問題