On the characteristics of a small C # example

sponsored links
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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of On the characteristics of a small C # example

  • ajax-engine

    Call the method is relatively simple

  • AJAX Packaging Tools

    Writing a javascript procedure ajax package put in a js file for your reference, please exhibitions. First statement of a few variables: Create XmlHttpRequest object: Implementation of Get / Post requests in the incoming Ways: Get the request method: ...

  • Ajax Portal

    The first test procedure: <html> <title>Full use XMLHttpRequest to load the document example </title> <head> <script type='text/javascript'> var req=null; var console=null; var READY_STATE_UNINITIALIZED=0; var READY_STATE_LOA

  • tomcat6.0 + myeclipse6.5 under tomcat data source configuration

    (1) Environment: such as a very important question! Different environment, different configurations! (2) struts package loaded: My MyEclipse6.5 bring him under the load of the struts jar package Error occurred "collections can not find under qui ...

  • javascript: history.go () and History.back () the difference between collections

    <input type=button value= Refresh > <input type=button value= Forward > <input type=button value= Back > <input type=button value= Forward > <input type=button value= Back > Back + refresh <input type=button value= Ba ...

  • ajax overtime

    ajax overtime

  • log4j easy application in java

    JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Message Dri

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

blog comments powered by Disqus
Recent Entries
Tag Cloud
Random Entries