【C#】C#进阶笔记

接下来是C#的进阶教程了,仍然是杂七杂八并不全面的随笔。

1. 特性(Attribute)

特性(Attribute)是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。您可以通过使用特性向程序添加声明性信息。
特性(Attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。

1.1 预定义特性

1.1.1 AttributeUsage

描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。语法如下:

[AttributeUsage(
	// 规定特性可被放置的语言元素。它是枚举器 AttributeTargets 的值的组合。默认值是 AttributeTargets.All。
   validon,
   // allowmultiple(可选的)为该特性的 AllowMultiple 属性(property)提供一个布尔值。如果为 true,则该特性是多用的。默认值是 false(单用的)。
   AllowMultiple=allowmultiple,
   //inherited(可选的)为该特性的 Inherited 属性(property)提供一个布尔值。如果为 true,则该特性可被派生类继承。默认值是 false(不被继承)。
   Inherited=inherited
)]
1.1.2 Conditional

这个暂时不了解,等后面如果实际需要的时候再补上。。。

1.1.3 Obsolete

标记了不应被使用的程序实体。它可以让您通知编译器丢弃某个特定的目标元素。例如,当一个新方法被用在一个类中,但是您仍然想要保持类中的旧方法,您可以通过显示一个应该使用新方法,而不是旧方法的消息,来把它标记为 obsolete(过时的)。例如:

class Program
    {
        /*
         * 参数 message,是一个字符串,描述项目为什么过时的原因以及该替代使用什么。
         * 参数 iserror,是一个布尔值。如果该值为 true,编译器应把该项目的使用当作一个错误。默认值是 false(编译器生成一个警告)。
         */
        [Obsolete("不要用旧方法啦",true)]
        static void OldMethod() {
            Console.WriteLine("这是一个旧方法");
        }
        static void NewMethod() {
            Console.WriteLine("这是新的方法");
        }
        static void Main(string[] args)
        {
            OldMethod();
            Console.ReadKey();
        }
    }

因为iserror属性设置为了true,所以在main方法调用OldMethod方法时报错,
在这里插入图片描述

1.2 自定义特性

1、创建一个自定义特性:

// 描述如何使用一个自定义特性 SomethingAttribute
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]    

//********自定义特性SomethingAttribute**************//
public class SomethingAttribute : Attribute    {
    private string name; // 名字
    private string data; // 日期
    public string Name {
        get { return name; }
        set { name = value; }
    }
    public string Data    {
        get { return data; }
        set { data = value; }
    }
    public SomethingAttribute(string name)    {
        this.name = name;
        this.name = name;
    }
}

2、实例化自定义

[Something("Amy", Data = "2018-06-18")]
[Something("Jack", Data = "2018-06-18")]
class Test{}

3、获取自定义特性的中的变量

Type t = typeof(Test);
var something = t.GetCustomAttributes(typeof(SomethingAttribute),true);
foreach(SomethingAttribute each in something)
{
    Console.WriteLine("Name:{0}", each.Name);
    Console.WriteLine("Data:{0}",each.Data);
}

2.反射(Reflection)

反射指程序可以访问、检测和修改它本身状态或行为的一种能力。

2.1 优缺点

优点:

  • 1、反射提高了程序的灵活性和扩展性。
  • 2、降低耦合性,提高自适应能力。
  • 3、它允许程序创建和控制任何类的对象,无需提前硬编码目标类。

缺点:

  • 1、性能问题:使用反射基本上是一种解释操作,用于字段和方法接入时要远慢于直接代码。因此反射机制主要应用在对灵活性和拓展性要求很高的系统框架上,普通程序不建议使用。
  • 2、使用反射会模糊程序内部逻辑;程序员希望在源代码中看到程序的逻辑,反射却绕过了源代码的技术,因而会带来维护的问题,反射代码比相应的直接代码更复杂。

2.2 反射的用途

  • 它允许在运行时查看特性(attribute)信息。
  • 它允许审查集合中的各种类型,以及实例化这些类型。
  • 它允许延迟绑定的方法和属性(property)。
  • 它允许在运行时创建新类型,然后使用这些类型执行一些任务。

2.3 使用反射来查看特性(attribute)信息

在一个需要查看的类前声明一个自定义特性,然后在其他的类中调用System.Reflection 类的 MemberInfo 对象。
例如:

[HelpAttribute("Information on the class MyClass")]
class MyClass
{
}

namespace AttributeAppl
{
   class Program
   {
      static void Main(string[] args)
      {
         System.Reflection.MemberInfo info = typeof(MyClass);
         object[] attributes = info.GetCustomAttributes(true);
         for (int i = 0; i < attributes.Length; i++)
         {
            System.Console.WriteLine(attributes[i]);
         }
         Console.ReadKey();
      }
   }
}

运行结果会显示附加到类 MyClass 上的自定义特性:

HelpAttribute

猜你喜欢

转载自blog.csdn.net/qq_34659777/article/details/82856826