C# 特性的使用

using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace AttributeExam
{
    class Program
    {
        private static readonly StringBuilder outputText = new StringBuilder(1000);
        private static DateTime backDateTo = new DateTime(2010, 2, 1);
        static void Main(string[] args)
        {
            Assembly theAssembly = Assembly.Load("VectorLibrary");
            Attribute supportsAttribute =
                Attribute.GetCustomAttribute(
                    theAssembly, typeof(SupportsWhatsNewAttribute));



            string name = theAssembly.FullName;


            AddToMessage("Assembly: " + name);
            if (supportsAttribute == null)
            {
                AddToMessage(
                    "This assembly does not support WhatsNew attributes");
                return;
            }
            else
            {
                AddToMessage("Defined Types:");
            }


            Type[] types = theAssembly.GetTypes();
            foreach (Type definedType in types)
                DisplayTypeInfo(definedType);


            MessageBox.Show(outputText.ToString(),
                            "What\'s New since " + backDateTo.ToLongDateString());
            Console.ReadLine();
        }


        private static void DisplayTypeInfo(Type type)
        {
            // make sure we only pick out classes
            if (!(type.IsClass))
                return;
            AddToMessage("\nclass " + type.Name);
            Attribute[] attribs = Attribute.GetCustomAttributes(type);
            if (attribs.Length == 0)
                AddToMessage("No changes to this class");
            else
                foreach (Attribute attrib in attribs)
                    WriteAttributeInfo(attrib);


            MethodInfo[] methods = type.GetMethods();
            AddToMessage("CHANGES TO METHODS OF THIS CLASS:");
            foreach (MethodInfo nextMethod in methods)
            {
                object[] attribs2 =
                    nextMethod.GetCustomAttributes(
                        typeof(LastModifiedAttribute), false);
                if (attribs2 != null)
                {
                    AddToMessage(
                        nextMethod.ReturnType + " " + nextMethod.Name + "()");
                    foreach (Attribute nextAttrib in attribs2)
                        WriteAttributeInfo(nextAttrib);
                }
            }
        }


        private static void WriteAttributeInfo(Attribute attrib)
        {
            L astModifiedAttribute lastModifiedAttrib =
                attrib as LastModifiedAttribute;

            if (lastModifiedAttrib == null)
                return;


            // check that date is in range
            DateTime modifiedDate = lastModifiedAttrib.DateModified;
            if (modifiedDate < backDateTo)
                return;


            AddToMessage("  MODIFIED: " +
                         modifiedDate.ToLongDateString() + ":");
            AddToMessage("    " + lastModifiedAttrib.Changes);
            if (lastModifiedAttrib.Issues != null)
                AddToMessage("    Outstanding issues:" +
                             lastModifiedAttrib.Issues);
        }


        private static void AddToMessage(string message)
        {
            outputText.Append("\n" + message);
        }
    }

}


using ClassLibrary;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


[assembly: SupportsWhatsNew]


namespace VectorLibrary
{
    [LastModified("14 Feb 2010", "IEnumerable interface implemented " +
                                "So Vector can now be treated as a collection")]
    [LastModified("10 Feb 2010", "IFormattable interface implemented " +
                                 "So Vector now responds to format specifiers N and VE")]

    public class Vector:IFormattable,IEnumerable
    {
         public double x, y, z;


        public Vector(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }




       [LastModified("10 Feb 2010", "Method added in order to provide formatting support")]
        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format == null)
                return ToString();
            string formatUpper = format.ToUpper();
            switch (formatUpper)
            {
                case "N":
                    return "|| " + Norm().ToString() + " ||";
                case "VE":
                    return String.Format("( {0:E}, {1:E}, {2:E} )", x, y, z);
                case "IJK":
                    StringBuilder sb = new StringBuilder(x.ToString(), 30);
                    sb.Append(" i + ");
                    sb.Append(y.ToString());
                    sb.Append(" j + ");
                    sb.Append(z.ToString());
                    sb.Append(" k");
                    return sb.ToString();
                default:
                    return ToString();
            }
        }


       public Vector(Vector rhs)
       {
           x = rhs.x;
           y = rhs.y;
           z = rhs.z;
       }


       [LastModified("14 Feb 2010", "Method added in order to provide collection support")]
       public IEnumerator GetEnumerator()
       {
           return new VectorEnumerator(this);
       }


       public override string ToString()
       {
           return "( " + x + " , " + y + " , " + z + " )";
       }


       public double this[uint i]
       {
           get
           {
               switch (i)
               {
                   case 0:
                       return x;
                   case 1:
                       return y;
                   case 2:
                       return z;
                   default:
                       throw new IndexOutOfRangeException(
                           "Attempt to retrieve Vector element" + i);
               }
           }
           set
           {
               switch (i)
               {
                   case 0:
                       x = value;
                       break;
                   case 1:
                       y = value;
                       break;
                   case 2:
                       z = value;
                       break;
                   default:
                       throw new IndexOutOfRangeException(
                           "Attempt to set Vector element" + i);
               }
           }
       }


       public static bool operator ==(Vector lhs, Vector rhs)
       {
           if (System.Math.Abs(lhs.x - rhs.x) < double.Epsilon &&
               System.Math.Abs(lhs.y - rhs.y) < double.Epsilon &&
               System.Math.Abs(lhs.z - rhs.z) < double.Epsilon)
               return true;
           else
               return false;
       }


       public static bool operator !=(Vector lhs, Vector rhs)
       {
           return !(lhs == rhs);
       }


       public static Vector operator +(Vector lhs, Vector rhs)
       {
           Vector result = new Vector(lhs);
           result.x += rhs.x;
           result.y += rhs.y;
           result.z += rhs.z;
           return result;
       }


       public static Vector operator *(double lhs, Vector rhs)
       {
           return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
       }


       public static Vector operator *(Vector lhs, double rhs)
       {
           return rhs * lhs;
       }


       public static double operator *(Vector lhs, Vector rhs)
       {
           return lhs.x * rhs.x + lhs.y + rhs.y + lhs.z * rhs.z;
       }


       public double Norm()
       {
           return x * x + y * y + z * z;
       }


       [LastModified("14 Feb 2010", "Class created as part of collection support for Vector")]
       private class VectorEnumerator : IEnumerator
       {
           readonly Vector _theVector;      // Vector object that this enumerato refers to 
           int _location;   // which element of _theVector the enumerator is currently referring to 


           public VectorEnumerator(Vector theVector)
           {
               _theVector = theVector;
               _location = -1;
           }


           public bool MoveNext()
           {
               ++_location;
               return (_location > 2) ? false : true;
           }


           public object Current
           {
               get
               {
                   if (_location < 0 || _location > 2)
                       throw new InvalidOperationException(
                           "The enumerator is either before the first element or " +
                           "after the last element of the Vector");
                   return _theVector[(uint)_location];
               }
           }


           public void Reset()
           {
               _location = -1;
           }


       }
    }

}


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


namespace ClassLibrary
{
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,
        AllowMultiple=true,
        Inherited=false)]
    public class LastModifiedAttribute:Attribute

    {
        private readonly DateTime _dateModified;


        public DateTime DateModified
        {
            get { return _dateModified; }
        }


        private readonly string _changes;


        public string Changes
        {
            get { return _changes; }
        }


        public string Issues { get; set; }


        public LastModifiedAttribute(string dateModified, string changes)
        {
            _dateModified = DateTime.Parse(dateModified);
            _changes = changes;
        }


    }

}



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


namespace ClassLibrary
{
    [AttributeUsage(AttributeTargets.Assembly)]
    public class SupportsWhatsNewAttribute:Attribute

    {
    }
}

转载于:https://www.cnblogs.com/dxmfans/p/9434794.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值