Reflection transmits actual combat (-) invokemember

xiaoxiao2021-03-06  106

The reflection is one of the advanced features in .NET, using reflection can achieve many of the functions that are incredible to be incredible. Here is a little practice for reflection after I have seen "Programming C #" (O'Reilly). The procedure will explain the problem, but it has been working hard and takes into account the use of simplicity. An example will now give an example.

The following program generates a Test.cs file at runtime, and calls CSC to compile into a Test.dll file, then use the type.invokemember () method to call the SayHello () method, and then compare the performance.

Using system; using system.io; using system.reflection; Namespace InvokeMember {///////

/// Class1 summary description. ///

Class

Class1 {

///

/// The main entry point for the application. ///

[Stathread]

Static

Void

Main

String

[] args) {

//

Cycles

Const

int

iTerations

=

100

;

//

Calculation time

Datetime StartTime

=

Datetime.now;

for

(

int

i

=

0

i

<

iTerations; i

) {

//

Control method

Console.writeline

"

Hello, World

"

} Timespan Elasped

=

Datetime.now

-

StartTime; console.writeline

"

Looping Elapsed MilliseConds:

"

Elasped.totalmilliseConds

"

For {0} iterations

"

, Items;

//

Use reflection transmission

REFLECTIONTEST T

=

New

ReflectionTest ();

//

Calculation time

StartTime

=

Datetime.now;

for

(

int

i

=

0

i

<

iTerations; i

) {T.dooperation ();} Elasped

=

Datetime.now

-

StartTime; console.writeline

"

Looping Elapsed MilliseConds:

"

Elasped.totalmilliseConds

"

For {0} iterations

"

, Items; console.readline ();}}

///

/// reflection's summary description. ///

public

Class

ReflectionTest {

//

Save the dynamically generated and compiled class TYPE objects

Type Thetpe

=

NULL

;

//

Save the dynamic generated instance

ObjectTheclass

=

NULL

;

///

/ /////// /

public

Void

DOOPERATION () {

//

Uninitialized

IF

(THTYPE

==

NULL

) {

//

initialization

GENERATECODE ();

//

Array when calling methods (here is empty)

Object

[] arguments

=

New

Object

[

0

];

//

Method for calling dynamically generated classes

Thetpe.invokemember

"

Sayhello

"

,

//

Method name to be called

Bindingflags.default

|

BindingFlags.InvokeMethod,

//

Binding logo, detailed See MSDN

NULL

,

//

Use the default binding object

THECLASS,

//

Call this method on the THECLASS instance

Arguments

//

Array array when calling methods

}

///

/// runtime generation code ///

Private

Void

Generatecode () {

//

file name

String

Filename

=

"

Test

"

;

//

Open the file, if you don't exist, create

Stream S

=

File.open (filename)

"

.CS

"

FileMode.create;

//

Create a streamwriter to write data

Streamwriter WRTR

=

New

StreamWriter (s);

//

Write dynamically created source code

WRTR.WRITELINE

"

// Dynamically create TEST classes

"

);

//

Class name

String

Classname

=

"

TestClass

"

WRTR.WRITELINE

"

Using system;

"

WRTR.WRITELINE

"

Class {0}

"

, ClassName); WRTR.WRITELINE

"

{

"

WRTR.WRITELINE

"

/ TPUBLIC VOID SAYHELLO ()

"

WRTR.WRITELINE

"

/ t {

"

WRTR.WRITELINE

"

/ / T /TCONSOLE.WRITELINE (/ "Hello, World/);

"

WRTR.WRITELINE

"

/ t}

"

WRTR.WRITELINE

"

}

"

);

//

Close streamwriter and files

WRTR.CLOSE (); S.Close ();

//

Startup process compilation source file

//

Specify parameters

ProcessStartInfo PSI

=

New

ProcessStartInfo ();

//

Start cmd.exe

PSI.FileName

=

"

cmd.exe

"

;

//

CMD.exe parameters, / c-close, close after completion; after the parameter, specify cmd.exe to use CSC to compile the source file String

CompileString

=

"

/ c: //winnt//microsoft.net/framework//V1.1.4322//csc.exe / optimize / target: library {0} .cs

"

Psi.arguments

=

String.Format (CompileString, FileName);

//

Running style - minimize

psi.windowstyle

=

ProcessWindowStyle.minimized;

//

Startup process

Process Proc

=

Process.start (PSI);

//

Specifies the current wait before this process

Proc.waitForexit ();

//

From compiled DLL file load an assemblyMbly

Assembly a

=

AskMBLY.LOADFROM (filename

"

.dll

"

);

//

Instances of creating classes

THECLASS

=

a.createInstance (classname);

//

Types of this type of instance

Thetpe

=

A.GetType (classname);

//

Delete source file

//

File.delete (FlieName ".cs");

}}}} Procedure: Hello, Worldhello, World.. Looping Elapsed Milliseconds: 93.75 for 100 Item Hello, Worldhello, World... Looping Elapsed Milliseconds: 2875 for 100 Iterations Performance does not dominate, mainly because Write files and compile work, but the later methods are optimized to performance, and performance will be greatly improved when the last method, but the last method is not as good as the first two.

转载请注明原文地址:https://www.9cbs.com/read-124070.html

New Post(0)