C # Study Notes 12,

Source: Internet
Author: User

C # Study Notes 12,

1.When Using Reflection, reflection can bypass the classes or attributes modified at the secure access level (private and protected) to obtain the required information.

2.Generic reflection: You can use the Type. ContainsGenericParameters attribute to determine whether a class or method contains a non-set generic real parameter. The Type. IsGenericType attribute indicates whether it is a generic Type.

3.Attribute: You can use attributes to modify classes and interfaces. Structure, enumeration, Delegate, event, attribute, field, method, constructor, indexer, parameter, type parameter, return value, assembly, module, there are two types of syntax using features, you can create multiple [feature types] or [feature types, feature types]. The preceding syntax mark can be used to list most of the structures, but this syntax is not suitable for "return values, assembly, and module ".

(1) assembly, [assembly: feature name].

(2) module, [module: feature name].

(3) return value, [return: feature name].

4.Most features are modified only for specific structures. To avoid improper use of features, you can use the [AttributeUsageAttribute (AttributeTargets. xxx)] feature class to mark feature limitations.

5.Named parameter: for example, [AttributeUsage (AttributeTargets. class, AllowMultiple = true)] syntax is different from the constructor initialization syntax, because the AttributeUsageAttribute Class does not contain constructors of two parameters, although C #4.0 supports named parameters, but it is also required to specify the method itself. The named parameter is used to set specific public attributes and fields in the feature constructor call. Even if the constructor does not contain the corresponding parameters, the named parameter is optional, however, it allows you to set additional instance data for features without providing a corresponding constructor parameter.

6.Serializable: in essence, the System. Runtime, Serialization. SerializationInfo object is a collection composed of "name/value" pairs. The SerializationInfo object is used in custom serialization (the ISerializable interface must be implemented.

7.Dynamic Programming (Dynamic): One of the key functions of reflection is to dynamically search for and call a member of a specific type, which requires identifying the member name or other features during execution. The Dynamic Programming-Dynamic added in C #4.0 provides a simpler way to call Members through reflection. However, the limit of this technology is that the member name and signature must be known during compilation. If this member is not found during execution, a RuntimeBinderException is thrown. You can view the UseDynamic. Test () code.

8.Dynamic: Dynamic is an Object. Any Object can be implicitly converted to Dynamic. Dynamic can be converted to other objects explicitly, so Dynamic is like an Object in behavior, similar to an Object, it even returns null (default (dynamic) for its default value )). Special dynamic behaviors of dynamic only occur during the call. This behavior is the key to distinguishing it from the Object. Any member call of dynamic will return the dynamic type, but if GetType () is executed on Dynamic, it will return the compiled type (that is, the type that is finally assigned to the dynamic variable ).

9.Implement custom dynamic objects: the key to defining custom dynamic types is to implement System. dynamic. IDynamicMetaObjectProvider interface, but it does not need to be implemented from scratch. Instead, the first solution is from System. dynamic. the DynamicObject class inherits and overrides the corresponding method. The DynamicObject class has implemented the IDynamicMetaObjectProvider interface and provides default processing. You can view the code DynamicXml class. Compare the two methods NormalMethod () and DynamicMethod () in UseDynamic class. The NormalMethod () method reads General xml data, and the DynamicMethod () method uses custom dynamic objects to parse xml content.

[Serializable] [AttributeUsage (AttributeTargets. Class, AllowMultiple = true)] public class MyDescriptionAttribute: Attribute {} public class UseDynamic {public static void Test () {dynamic data = "Hello! My name is Inigo Montoya "; Console. writeLine (data); data = (double) data. length; data = data * 3.5 + 28.6; if (data = 2.4 + 112 + 26.2) {Console. writeLine ("data for length: {0}", data);} else {data. nonExistentMethodCallStillCompiles () ;}} public static void NormalMethod () {XElement person = XElement. parse ("<Person> <Name> main god </Name> <Age> 26 </Age> </Person>"); Console. writeLine ("{0}, {1}", person. D Escendants ("Name "). firstOrDefault (). value, person. descendants ("Age "). firstOrDefault (). value);} public static void DynamicMethod () {dynamic person = DynamicXmL. parse ("<Person> <Name> main god </Name> <Age> 26 </Age> </Person>"); Console. writeLine ("{0}, {1}", person. name, person. age) ;}//< summary> /// custom Dynamic Object /// </summary> public class DynamicXmL: DynamicObject {private XElement element; public DynamicXmL (XElement xElement) {element = xElement;} public static dynamic Parse (string text) {return new DynamicXmL (XElement. parse (text);} public override bool TryGetMember (GetMemberBinder binder, out object result) {bool success = false; result = null; XElement firstDescendant = element. descendants (binder. name ). firstOrDefault (); if (firstDescendant! = Null) {if (firstDescendant. descendants (). count ()> 0) {result = new DynamicXmL (firstDescendant);} else {result = firstDescendant. value;} success = true;} return success;} public override bool TrySetMember (SetMemberBinder binder, object value) {bool success = false; XElement firstDescendant = element. descendants (binder. name ). firstOrDefault (); if (firstDescendant! = Null) {if (value. getType () = typeof (XElement) {firstDescendant. replaceWith (value);} else {firstDescendant. value = value. toString () ;}success = true;} return success ;}}
View Code

----------------- The above content is organized according to "C # the third edition of this topic ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.