On the characteristics of a small C # example

Recent statements to write a network.

As we all know, the network programmers report is a big nightmare.

Fortunately, the next needs to understand, then to read the following statements about the network related information.

RDLC report feeling that comes with Microsoft to fully meet the demand.

However, I always hurt.

Boss saying: "To make component, the next call to direct drag on the line, no manual!"


I ask: is the dark horse of God feel? !

This means that to completely dynamically generated statements (do not know rdlc statements to operate under their shoes to try to know).

Which use the techniques, I probably thought for a moment.

XML structure is mainly the definition and serial number, anti-sequence words. Other no. However, these two technologies to make me feel suddenly fell into the hell (the two I do not know..)

No recourse but to bite the bullet and did.

The first problem encountered is the c # properties mechanism.

Slightly glanced at, understand, and to some of the first record from that classic example of it (2 hours ago, I even have this [] is called features do not know how or have not seen this in the operator 2. Several hours of reading the code, especially in the following sample code to understand!).

Not more than that, as follows:

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);
            foreach (System.Attribute attribute in customAttributes)
                if (attribute is BoardingCheckAttribute)
                    boardingCheck = attribute as BoardingCheckAttribute;
                        + "'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
                        + ".");

    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 |
                       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()

    private static void PrintAuthorInfo(System.Type t)
        System.Console.WriteLine("Author information for {0}", t);
        System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // reflection

        foreach (System.Attribute attr in attrs)
            if (attr is Author)
                Author a = (Author)attr;
                System.Console.WriteLine("   {0}, version {1:f}", a.GetName(), a.version);

Search the web for explanation: (corresponding to the second paragraph of the code)

Accessing Attributes With Reflection (C # Programming Guide)
If you do not retrieve the custom attribute information and its method of operation, then define custom properties and place it in the source code of no significance. C # has a reflective system can be used to retrieve the custom attributes defined by the information. Main method is GetCustomAttributes, it returns the object array , these objects at run time equivalent to the source code attributes. This method has several overloaded versions. For more information, see Attribute.

Property norms, such as:

C #
[Author ("H. Ackerman", version = 1.1)]
class SampleClass

Conceptually equivalent to:

C #
Author anonymousAuthorObject = new Author ("H. Ackerman");
anonymousAuthorObject.version = 1.1;

【Preparation: I first saw this writing, and then hurried to try Hing the next class of property, found that the wording is not used.

Wondering: Is this wording applies only to properties instantiated it? If the master to see, please also advise. 】

However, to obtain the property until the check SampleClass do this only when the code. GetCustomAttributes of SampleClass call as described above will lead to an Author object is constructed and initialized. If the class there are other attributes, other attributes object constructed in a similar manner. Author GetCustomAttributes object and then return any other property of the array object. This array can then iterate to determine according to each array element 's type attribute of the application, and extract information from the attributes object.

分类:DotNet 时间:2011-05-26 人气:209
blog comments powered by Disqus


  • StringUtils类:isEmpty(String str) VS isBlank(String 2015-05-05

    org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String str). StringUtils.isEmpty(String str) 功能: 判断某字符串是否为"空或长度为0",str==null 或 str.length()==0 示例: public static void main(String[] args) { System.out.pri

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • How the eclipse of the main the String [] args parameter passed, and then export it 2010-12-26

    How the eclipse of the main the String [] args parameter passed, and then export it? In the Run menu to find Open Run dialog opens Select Java Application in the program need to run Select the Arguments Tab Enter the parameters in the Programe Argume

  • 浅析C#中的Main(String[] args)参数输入问题 2014-07-20

    本篇文章主要是对C#中的Main(String[] args)参数输入问题进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 指定相关的测试代码 首先,写一个用于测试的关于Main(String[] args)参数输入有关的代码类,如下: using System; public class Hello { public static void Main(String[] args) { switch (args[0]) { case "老板": Console.Write

  • String [] args = new String [] {sql.toString ()} 2011-06-29

    In this problem StringBuffer sql = new StringBuffer (); Which sql.toString results: 'Test User 1', 'male' String [] args = new String [] {sql.toString ()} args.length 1 results Obviously not what we need. Note: new String []{"","",&quo

  • Spring property into the (int, String, List, Map, Set, String []) 2010-09-24

    1.JavaBean import java.util.List; import java.util.Map; import java.util.Set; public class Test ( private int intValue; private String strValue; private List listValue; private Map mapValue; private Set <String> setValue; private String [] arrayValu

  • String Manipulation - split a string by string 2010-03-30

    /* * Note the string decomposition function. strtok The more complex the application to properly. ! * You can also not strtok function , But to achieve string by splitting a string into difficult ! * Note the str to space must be readable and writabl

  • JavaScript中的object转换成number或string规则介绍 2014-06-10

    这篇文章主要介绍了JavaScript中的object转换成number或string规则介绍,本文讲解了object自动转换成string的规则.object自动转换成number的规则等内容,需要的朋友可以参考下 在JavaScript程序编写过程中,根据不同的上下文,JS会自动将object转换成number或者string后再处理.这种自动转换的规则如下: object自动转换成string的规则: 1.如果object所属类覆写了toString()方法,则调用该方法.如果toStri

  • vector char* 和 string和C-Style的string的效率 2012-03-26

    大部分人使用vector构造字符串数组,都是用vector<string>,但是也不乏人使用vector<char*> ,用vctor<char*>不会遇到任何问题,但是既然用了C的东西,很多东西需要自己来控制,不像C++的string那样: std::string is a string class, encapsulating all the required data that makes up a string, along with allocation an

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.238 (s). 12 q(s)