日期:2014-05-17  浏览次数:20916 次

关于C#中的特性小例子

最近要写一个网络报表。

众所周知,网络报表是程序员的一大噩梦。

幸运的是,了解了下需求,然后去网络上大概看了下报表相关的信息。

感觉微软自带的RDLC报表能完全满足需求了。

不过,受伤的总是我。

老大一句:“要做成组件,下次调用直接拖拉就行,不需要手动操作!”

...

请问:天黑是神马感觉?!

这句话的意思就是要完全动态生成报表(不了解rdlc报表的童鞋们去操作下试试就知道了)。

里面要用到的技术,我大概想了一下。

主要就是XML的架构定义和序列号,反序列话。其他的没什么。不过,就这2个技术,让我感觉顿时落入了地狱(两个我都不熟悉。)。

没办法,只能硬着头皮搞了。

第一个遇到的问题就是c#特性机制了。

稍微看了下,懂了,就先记录下一些自认为经典的例子吧(2个小时前,我连这个[]叫特性都不知道,或者说都没怎么见过这个运算符这个用。2个小时看了几个代码,尤其是下面示例的代码,就懂了!)。

?

不多说,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BoardingCheckAttribute boardingCheck = null;
            object[] customAttributes = typeof(HumanPropertyBase).GetCustomAttributes(true);
            Console.WriteLine(typeof(HumanPropertyBase).IsSerializable);
            foreach (System.Attribute attribute in customAttributes)
            {
                if (attribute is BoardingCheckAttribute)
                {
                    boardingCheck = attribute as BoardingCheckAttribute;
                    Console.WriteLine(boardingCheck.Name
                        + "'s ID is "
                        + boardingCheck.ID
                        + ", he/she wants to "
                        + boardingCheck.Destination
                        + " from "
                        + boardingCheck.Departure
                        + " by the plane "
                        + boardingCheck.FlightNumber
                        + ", his/her position is "
                        + boardingCheck.PositionNumber
                        + ".");
                }
            }

            Console.ReadLine();
        }
    }
    [Serializable]
    [BoardingCheckAttribute("123","louis",22,12,"aaaa","ccccc")]
    public class HumanPropertyBase
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Gender { get; set; }

        [Obsolete("I'm so old, don't kill me!", true)]
        public void Run(int speed)
        {
            // Running is good for health.  
        }
    }

    public class BoardingCheckAttribute : Attribute
    {
        public string ID { get; private set; }
        public string Name { get; private set; }
        public int FlightNumber { get; private set; }
        public int PositionNumber { get; private set; }
        public string Departure { get; private set; }
        public string Destination { get; private set; }

        public BoardingCheckAttribute(string id, string name, int flightNumber, int positionNumber, string departure, string destination)
        {
            this.ID = id;
            this.Name = name;
            this.FlightNumber = flightNumber;
            this.PositionNumber = positionNumber;
            this.Departure = departure;
            this.Destination = destination;
        }

    }

}

?

?

[System.AttributeUsage(System.AttributeTargets.Class |
                       System.AttributeTargets.Struct,
                       AllowMultiple = true)  // multiuse attribute
]
public class Author : System.Attribute
{
    string name;
    public double version;

    public Author(string name)
    {
        this.name = name;
        version = 1.0;  // Default value
    }

    public string GetName()
    {
        return name;
    }
}

[Author("H. Ackerman")]
public class FirstClass
{
    // ...
}

// No Author attribute
public class SecondClass
{
    // ...
}

[Author("H. Ackerman"), Author("M. Knott", version = 2.0)]
public class ThirdClass
{
    // ...
}

class TestAuthorAttribute
{
    static void Main()
    {
        PrintAuthorInfo(typeof(FirstClass));
        PrintAuthorInfo(typeof(SecondClass));
        PrintAuthorInfo(typeof(ThirdClass));
    }

    private static void PrintAuthorInfo(Sy