Today saw this code snippet below:

using
 System;

using
 System.Reflection;


namespace
 Donis.CSharpBook{
    

public
 
class
 Starter{
        

public
 
static
 
void
 Main(
string
 [] argv){
            Assembly executing

=
Assembly.GetExecutingAssembly();
            Type t

=
executing.GetType(argv[
0
]);
            Array zArray

=
Array.CreateInstance(
                t, argv.Length


2
);
            

for
(
int
 count
=
2
;count
<
argv.Length;
++
count) {
                System.Object obj

=
Activator.CreateInstance(t, 
new
 
object
[] {
                    argv[count]});
                zArray.SetValue(obj, count


2
);
            }
            

foreach
(
object
 item 
in
 zArray) {
                MethodInfo m

=
t.GetMethod(argv[
1
]);
                m.Invoke(item, 

null
);
            }
      }
    }

    


public
 
class
 ZClass {
        

public
 ZClass(
string
 info) {
            m_Info

=

ZClass 

+
info;
        }

        


public
 
void
 ShowInfo() {

            Console.WriteLine(m_Info);
        }

        


private
 
string
 m_Info;
    }

    


public
 
class
 YClass {
        

public
 YClass(
string
 info) {
            m_Info

=

YClass 

+
info;
        }

        


public
 
void
 ShowInfo() {
            Console.WriteLine(m_Info);
        }

        


private
 
string
 m_Info;
    }

    


public
 
class
 XClass {
        

public
 XClass(
string
 info) {
            m_Info

=

XClass 

+
info;
        }

        


public
 
void
 ShowInfo() {
            Console.WriteLine(m_Info);
        }

        


private
 
string
 m_Info;
    }
}

 

There are some code detais I make notes:

1. Assembly Class

quoted the article from MSDN:

Represents an assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

Remarks:

Use the Assembly class to load assemblies, to explore the metadata and constituent parts of assemblies, to discover the types contained in assemblies, and to create instances of those types.

To get an Assembly object for the currently executing assembly, use the GetExecutingAssembly method.

The GetTypes method lists all the types in the assembly. The GetExportedTypes method lists the types that are visible to callers outside the assembly. The GetType method can be used to search for a particular type in the assembly. The CreateInstance method can be used to search for and create instances of types in the assembly.

2.Activator Class

Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects. This class cannot be inherited.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

The CreateInstance method creates an instance of a type defined in an assembly by invoking the constructor that best matches the specified arguments. If no arguments are specified then the constructor that takes no parameters, that is, the default constructor, is invoked.

You must have sufficient permission to search for and call a constructor, otherwise an exception is thrown. By default, only public constructors are considered during the search for a constructor. If no constructor or default constructor can be found, an exception is thrown.

CreateInstance method

Activator.CreateInstance (Type, Object[])

Creates an instance of the specified type using the constructor that best matches the specified parameters.

3.Type Class

Represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Remarks:

Type is the root of the

System.Reflection
functionality and is the primary way to access metadata. Use the members of Type to get information about a type declaration, such as the constructors, methods, fields, properties, and events of a class, as well as the module and the assembly in which the class is deployed.

The C# typeof operator (GetType operator in Visual Basic, typeid operator in Visual C++) returns a Type object.

A Type object that represents a type is unique; that is, two Type object references refer to the same object if and only if they represent the same type. This allows for comparison of Type objects using reference equality.

Without

ReflectionPermission
, code can access only the public members of loaded assemblies. This includes, but is not limited to, unrestricted access to Object.GetType, access to public exported types through Type.GetType, and access to GetTypeFromHandle. Some properties of Type, such as FullName and Attributes, are accessible without ReflectionPermission.

Type is an abstract base class that allows multiple implementations. The system will always provide the derived class RuntimeType. In reflection, all classes beginning with the word Runtime are created only once per object in the system and support comparison operations.

This class is thread safe; multiple threads can concurrently read from an instance of this type. An instance of Type can represent any of the following types:

  • Classes

  • Value types

  • Arrays

  • Interfaces

  • Pointers

  • Enumerations

  • Constructed generic types and generic type definitions

  • Type arguments and type parameters of constructed generic types, generic type definitions, and generic method definitions

Type.GetMethod Method

Type.GetMethod (String)

Searches for the public method with the specified name.

Supported by the .NET Compact Framework.

Syntax:
public 
 
 
  
  MethodInfo
 
  GetMethod (
	string name
)
Type.GetMethods ()

Returns all the public methods of the current Type.

Supported by the .NET Compact Framework.

Syntax:

C#
public 
     
     
      
      MethodInfo
     
     [] GetMethods ()

4.MethodInfo Class

Discovers the attributes of a method and provides access to method metadata.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

MethodBase.Invoke (Object, Object[])

Invokes the method or constructor represented by the current instance, using the specified parameters.

Supported by the .NET Compact Framework.


C#
public 
       
       
        
        Object
       
        Invoke (
	
       
       
        
        Object
       
        obj,
	
       
       
        
        Object
       
       [] parameters
)




 

Parameters

obj

The object on which to invoke the method or constructor. If a method is static, this argument is ignored. If a constructor is static, this argument must be a null reference (Nothing in Visual Basic) or an instance of the class that defines the constructor.

parameters

An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, parameters should be a null reference (Nothing in Visual Basic).

If the method or constructor represented by this instance takes a ref parameter (ByRef in Visual Basic), no special attribute is required for that parameter in order to invoke the method or constructor using this function. Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is a null reference (Nothing in Visual Basic). For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.

 

 

Return Value

An object containing the return value of the invoked method, or a null reference (Nothing in Visual Basic) in the case of a constructor.

Familiar with english article.


版权声明:本文为waterathena原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/waterathena/article/details/1873816