當前位置: 首頁>>代碼示例>>C#>>正文


C# PropertyAttributes枚舉代碼示例

本文整理匯總了C#中System.Reflection.PropertyAttributes枚舉的典型用法代碼示例。如果您正苦於以下問題:C# PropertyAttributes枚舉的具體用法?C# PropertyAttributes怎麽用?C# PropertyAttributes使用的例子?那麽, 這裏精選的枚舉代碼示例或許可以為您提供幫助。


在下文中一共展示了PropertyAttributes枚舉的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: if

//引入命名空間
using System;
using System.Reflection;
 
 // Define three properties: one read-write, one default,
 // and one read only. 
public class Aproperty  
    // Define a read-write property.
{
    private string caption = "A Default caption";
    public string Caption
    {
        get{return caption;}
        set
        {
            if (caption != value){caption = value;}
        }
    }
}
public class Bproperty  
    // Define a default property.
{
    private string caption  = "B Default caption";
    public string this [int index]
    {
        get {return "1";}
    }
    public string Caption
    {
  
        get{return caption;}
        set
        {
            if (caption != value){caption = value;}
        }
    }
}
public class Cproperty  
    // Define a read-only property.
{
    private string caption = "C Default caption";
    public string Caption
    {
        get{return caption;}
        // No setting is allowed, because this is a read-only property.
    }
}
  
class propertyattributesenum
{
    public static int Main(string[] args)
    {
        Console.WriteLine("\nReflection.PropertyAttributes");
  
        // Determine whether a property exists, and change its value.
        Aproperty Mypropertya = new Aproperty();
        Bproperty Mypropertyb = new Bproperty();
        Cproperty Mypropertyc = new Cproperty();

        Console.Write("\n1. Mypropertya.Caption = " + Mypropertya.Caption );
      
        Console.Write("\n1. Mypropertyb.Caption = " + Mypropertyb.Caption );
      
        Console.Write("\n1. Mypropertyc.Caption = " + Mypropertyc.Caption );
  
        // Only Mypropertya can be changed, as Mypropertyb is read-only.
        Mypropertya.Caption = "A- This is changed.";
        Mypropertyb.Caption = "B- This is changed.";
        // Note that Mypropertyc is not changed because it is read only
  
        Console.Write("\n\n2. Mypropertya.Caption = " + Mypropertya.Caption );
  
        Console.Write("\n2. Mypropertyb.Caption = " + Mypropertyb.Caption );
 
        Console.Write("\n2. Mypropertyc.Caption = " + Mypropertyc.Caption );
  
        // Get the PropertyAttributes enumeration of the property.
        // Get the type.
        Type MyTypea = Type.GetType("Aproperty");
        Type MyTypeb = Type.GetType("Bproperty");
        Type MyTypec = Type.GetType("Cproperty");
  
        // Get the property attributes.
        PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
        PropertyAttributes Myattributesa = Mypropertyinfoa.Attributes;
        PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Item");
        PropertyAttributes Myattributesb = Mypropertyinfob.Attributes;
        PropertyInfo Mypropertyinfoc = MyTypec.GetProperty("Caption");
        PropertyAttributes Myattributesc = Mypropertyinfoc.Attributes;
  
        // Display the property attributes value.
      
        Console.Write("\n\na- " + Myattributesa.ToString());

        Console.Write("\nb- " + Myattributesb.ToString());

        Console.Write("\nc- " + Myattributesc.ToString());
        return 0;
    }
}

// This example displays the following output to the console
//
// Reflection.PropertyAttributes
//
// 1. Mypropertya.Caption = A Default caption
// 1. Mypropertyb.Caption = B Default caption
// 1. Mypropertyc.Caption = C Default caption
//
// 2. Mypropertya.Caption = A- This is changed.
// 2. Mypropertyb.Caption = B- This is changed.
// 2. Mypropertyc.Caption = C Default caption
//
// a- None
// b- None
// c- None
開發者ID:.NET開發者,項目名稱:System.Reflection,代碼行數:116,代碼來源:PropertyAttributes


注:本文中的System.Reflection.PropertyAttributes枚舉示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。