当前位置: 首页>>代码示例>>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;未经允许,请勿转载。