Showing posts with label Reflection. Show all posts
Showing posts with label Reflection. Show all posts

Friday, October 20, 2006

Using MemberInfo of System.Reflection

@ You need to ascertain wheter a method named OneMethod is accessible to a derived class. What shoud you do?
^ Call the IsFamily property of the MethodInfo Class.

@ The IsFamily property gets a value indicating whether the visibility of this method or constructor is described by MethodAttributes.Family; that is, the method or constructor is visible only within its class and derived classes. (Inherited from MethodBase.)
^ Family Indicates that the method is accessible only to members of this class and its derived classes.

^ The MethodAttribute enumeration also contains:
Abstract Indicates that the class does not provide an implementation of this method.
Assembly Indicates that the method is accessible to any class of this assembly.
Final Indicates that the method cannot be overridden.
Private Indicates that the method is accessible only to the current class.
Virtual Indicates that the method is virtual
Static Indicates that the method is defined on the type; otherwise, it is defined per instance.

e You can read more about MethodAttributes's member here.

e About MethodInfo class

Thursday, October 19, 2006

Reflecting types

The Type class有什么用?

@ The Type class represents a single type, allowing you to look at the methods, properties, events, interfaces, and inheritance tree of a particular type in the system.

How to get the Type object?

@ You can get Type object in 4 ways:

  1. From the Assembly class
  2. From the Module class
  3. From the instance of an object
  4. Using keyword typeof in C# or GetType in VB.

@ 1st way: from the Assembly class:

Dim OneAssembly As Assembly = Assembly.GetExecutingAssembly()
Dim AssemblyTypes() As Type = OneAssembly.GetTypes()

@ 2nd way: from the Module class:

Dim AllModulesOfOneAssembly As [Module] = OneAssembly.GetModules()
Dim OneModuleOfOneAssembly As [Module] = AllModulesOfOneAssembly(0)
Dim AllTypesOfOneModuleOfOneAssembly () As Type = OneModuleOfOneAssembly .GetTypes()

@ 3rd way: get the type of the instance of an object by calling its GetType method:

Dim OneObject As New Object()
Dim TypeOfOneObject As Type = OneObject.GetType()

@ 4th way: create a Type object using C# typeof / VB GetType keywork:

Dim OneSpecificType As Type = GetType(Int32) 'VB

Type OneSpecificType = typeof(Int32) //C#

Tuesday, October 10, 2006

Reflection到底是个啥东西?

@ Code in the CLR is packaged in an assembly. Even though it is often thought of as a file, an assembly is actually a logical container for different parts of the data the CLR needs to execute code.

@ These parts of data of an assembly include: Assembly metadata, Type metadata, Intermediate Language code, and Resources.

@ Assembly Metadata includes data that defined the assembly, such as the name, version, strong name, and culture information. Assembly metadata is also called manifest.

@ Type Metadata is the information describes what a type looks like, including the namespace, class name, members (methods, properties, constructors, etc.), and parameters.

@ Most of the time, all these parts of an assembly are compiled into a single file.

@ Assemblies are containers for modules, and modules are containers for types.

^ 我啥时候会用的上Reflection呀?

e C-sharp reflection: Save development time throughout the project life-cycle