5-8万年薪顶级嵌入式,京沪深就业地 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

关于.net componet和attributes,c#高手请进

楼主longbow74()2002-05-28 17:06:58 在 .NET技术 / C# 提问

最近在学C#,对.net   componet和attributes的用途不是很清楚,他们的功能和特点是什么,一般应该在什么时候用,请指点一二。  
  这方面的书好像满少的,都是些介绍语法的,我觉得语法其实不重要,明白什么时候该用它才是重要的。 问题点数:100、回复次数:6Top

1 楼snewxf(心疤)回复于 2002-05-28 18:21:33 得分 50

attributes我理解为附加信息。  
  比如说一个方法或是类你要告诉别人他有什么功能。。。。你就用这个附加说明。然后用反射求出。。。  
  component是组件的意思。  
  组件是由类来实现的。但组件和类不同。组件必须实现。system.componentModel.IComponent接口,并且提供一个没有参数的构造函数!假如一个类实现了这个接口。那么他就成组件了。  
  Top

2 楼longbow74()回复于 2002-05-29 10:34:19 得分 0

1.不知道system.componentModel.IComponent内部实现了什么,功能和原来的COM+有什么不同?  
  2.对用法还是不太明白,能结合一下MSDN的例子说一下吗?  
  //   AttributesTutorial.cs  
  //   This   example   shows   the   use   of   class   and   method   attributes.  
   
  using   System;  
  using   System.Reflection;  
  using   System.Collections;  
   
  //   The   IsTested   class   is   a   user-defined   custom   attribute   class.  
  //   It   can   be   applied   to   any   declaration   including  
  //     -   types   (struct,   class,   enum,   delegate)  
  //     -   members   (methods,   fields,   events,   properties,   indexers)  
  //   It   is   used   with   no   arguments.  
  public   class   IsTestedAttribute   :   Attribute  
  {  
          public   override   string   ToString()  
          {  
                  return   "Is   Tested";  
          }  
  }  
   
  //   The   AuthorAttribute   class   is   a   user-defined   attribute   class.  
  //   It   can   be   applied   to   classes   and   struct   declarations   only.  
  //   It   takes   one   unnamed   string   argument   (the   author's   name).  
  //   It   has   one   optional   named   argument   Version,   which   is   of   type   int.  
  [AttributeUsage(AttributeTargets.Class   |   AttributeTargets.Struct)]  
  public   class   AuthorAttribute   :   Attribute  
  {  
          //   This   constructor   specifies   the   unnamed   arguments   to   the   attribute   class.  
          public   AuthorAttribute(string   name)  
          {  
                  this.name   =   name;  
                  this.version   =   0;  
          }  
   
          //   This   property   is   readonly   (it   has   no   set   accessor)  
          //   so   it   cannot   be   used   as   a   named   argument   to   this   attribute.  
          public   string   Name    
          {  
                  get    
                  {  
                          return   name;  
                  }  
          }  
   
          //   This   property   is   read-write   (it   has   a   set   accessor)  
          //   so   it   can   be   used   as   a   named   argument   when   using   this  
          //   class   as   an   attribute   class.  
          public   int   Version  
          {  
                  get    
                  {  
                          return   version;  
                  }  
                  set    
                  {  
                          version   =   value;  
                  }  
          }  
   
          public   override   string   ToString()  
          {  
                  string   value   =   "Author   :   "   +   Name;  
                  if   (version   !=   0)  
                  {  
                          value   +=   "   Version   :   "   +   Version.ToString();  
                  }  
                  return   value;  
          }  
   
          private   string   name;  
          private   int   version;  
  }  
   
  //   Here   you   attach   the   AuthorAttribute   user-defined   custom   attribute   to    
  //   the   Account   class.   The   unnamed   string   argument   is   passed   to   the    
  //   AuthorAttribute   class's   constructor   when   creating   the   attributes.  
  [Author("Joe   Programmer")]  
  class   Account  
  {  
          //   Attach   the   IsTestedAttribute   custom   attribute   to   this   method.  
          [IsTested]  
          public   void   AddOrder(Order   orderToAdd)  
          {  
                  orders.Add(orderToAdd);  
          }  
   
          private   ArrayList   orders   =   new   ArrayList();  
  }  
   
  //   Attach   the   AuthorAttribute   and   IsTestedAttribute   custom   attributes    
  //   to   this   class.  
  //   Note   the   use   of   the   'Version'   named   argument   to   the   AuthorAttribute.  
  [Author("Jane   Programmer",   Version   =   2),   IsTested()]  
  class   Order  
  {  
          //   add   stuff   here   ...  
  }  
   
  class   MainClass  
  {  
        private   static   bool   IsMemberTested(MemberInfo   member)  
        {  
                  foreach   (object   attribute   in   member.GetCustomAttributes(true))  
                  {  
                          if   (attribute   is   IsTestedAttribute)  
                          {  
                                return   true;  
                          }  
                  }  
              return   false;  
        }  
   
          private   static   void   DumpAttributes(MemberInfo   member)  
          {  
                  Console.WriteLine("Attributes   for   :   "   +   member.Name);  
                  foreach   (object   attribute   in   member.GetCustomAttributes(true))  
                  {  
                          Console.WriteLine(attribute);  
                  }  
          }  
   
          public   static   void   Main()  
          {  
                  //   display   attributes   for   Account   class  
                  DumpAttributes(typeof(Account));  
   
                  //   display   list   of   tested   members  
                  foreach   (MethodInfo   method   in   (typeof(Account)).GetMethods())  
                  {  
                          if   (IsMemberTested(method))  
                          {  
                                Console.WriteLine("Member   {0}   is   tested!",   method.Name);  
                          }  
                          else  
                          {  
                                Console.WriteLine("Member   {0}   is   NOT   tested!",   method.Name);  
                          }  
                  }  
                  Console.WriteLine();  
   
                  //   display   attributes   for   Order   class  
                  DumpAttributes(typeof(Order));  
   
                  //   display   attributes   for   methods   on   the   Order   class  
                  foreach   (MethodInfo   method   in   (typeof(Order)).GetMethods())  
                  {  
                        if   (IsMemberTested(method))  
                        {  
                                Console.WriteLine("Member   {0}   is   tested!",   method.Name);  
                        }  
                        else  
                        {  
                                Console.WriteLine("Member   {0}   is   NOT   tested!",   method.Name);  
                        }  
                  }  
                  Console.WriteLine();  
          }  
  }  
   
  Output  
  Attributes   for   :   Account  
  Author   :   Joe   Programmer  
  Member   GetHashCode   is   NOT   tested!  
  Member   Equals   is   NOT   tested!  
  Member   ToString   is   NOT   tested!  
  Member   AddOrder   is   tested!  
  Member   GetType   is   NOT   tested!  
   
  Attributes   for   :   Order  
  Author   :   Jane   Programmer   Version   :   2  
  Is   Tested  
  Member   GetHashCode   is   NOT   tested!  
  Member   Equals   is   NOT   tested!  
  Member   ToString   is   NOT   tested!  
  Member   GetType   is   NOT   tested!  
  Top

3 楼alpine(笨笨)回复于 2002-05-29 11:28:25 得分 50

昨天给你回了一篇,结果没有粘上,分特勒  
  Attribute的用处就是保存那些与类或者方法本身相关的,可是又不是类或者方法自身的东东。在Design   Time(设计阶段,也可以认为是编码阶段)可以确定的,硬编码进去;在Run   Time(运行阶段)可以读出,但是并不能修改(不太确定)。以前一般来说,这种工作都是放在配置文件中或者资源文件中进行保存的,如果不需要在运行阶段读出的话,可以放在注释中。现在都可以统一放在Attribute里面了。  
  例如:有些值需要保存在注册表中,在注册表中的位置是确定的,但也要易于修改。以前会放在字符串资源里面,或者单独有配置文件。现在都可以放在Attribute里面。上面的例子里面有Author的用法吧,以前是放在注释里面,现在可以放在Attribute里面。还有WebMethod,用Attribute说明是非常合适的。Top

4 楼longbow74()回复于 2002-05-29 17:02:26 得分 0

有点点懂了,除了些文字性的说明如用法,版本,作者之类的东东一般还放什么东东?Top

5 楼longbow74()回复于 2002-05-30 16:30:01 得分 0

component?Top

6 楼longbow74()回复于 2002-05-31 10:07:30 得分 0

again?Top

相关问题

  • VC++.NET和C#.NET
  • VISUAL C#.NET与VISUAL C++.NET啥区别?
  • C#.NET与C++.NET有什么区别
  • 想学Visual C++.NET
  • C#.net做网站
  • C,C++ VC,VC++,VC++.NET,的疑惑
  • 请问C# Attributes的作用与用法!
  • 在Visual C++ .Net下怎么编标准C/C++的程序呢?
  • 如何用c#.net聲明這個API函數(C轉C#)
  • c#?!?!?!

关键词

  • .net
  • c#
  • 组件
  • authorattribute
  • attribute
  • member
  • istestedattribute
  • dumpattributes
  • istested
  • ismembertested

得分解答快速导航

  • 帖主:longbow74
  • snewxf
  • alpine

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
x 提问