2012年3月21日星期三

Monster HeadphonesLike DotNet

Like DotNet,Java, language can make dynamic proxy class is created, thanks to its itself is not directly compiled into machine code, but compiled into intermediate language ,at run-time interpretation or dynamic compilation into machine language .
That is why these concepts in Java the basic reason for the rising of .Dynamic proxy class ,generally are used in the Emit namespace directive ,but the IL is more demanding ,I here using C # provides dynamic compilation function realization ,Monster Beats Studio,has the advantages of intuitive ,easy to understand ,not familiar with the IL directive ,Monster Headphones,shortcomings of course is not very professional .
( many online using Emit ,IL instructions to construct dynamic proxy class code ) to dynamic proxy ( I tend to use the decoration ) ,Vibram Five Fingers,a key place requires that your class needs to be monitored ( hook ) member is at least guaranteed support virtual methods.
The following is dynamic proxy class factory ,here introduces this class ,behind the post will explain why to do so :/ / proxy class factory ,simple factory method .Public class MyProxyFactory {object private List< ;_ target ;IMethodInterceptor> ;adviceList = new List< _ ;IMethodInterceptor> ;( ) ;/ / the so-called front ,rear and notification class ,in fact is to be injected into the spy .
Public void Add ( IMethodInterceptor advice ) { _ adviceList.Add (advice ) ;} / / constructor ,incoming target object. Public MyProxyFactory ( object target ) { _ target = target ;} / / create and put back the proxy class .
Note ,here is always created, can actually be cached .Public object GetProxyClass ( ) {StringBuilder theClassBuilder = new StringBuilder ( ) ;Type theTargetType = target.GetType ( string ) _ ;theProxyClassName = " ;" _ _ ;+ theTargetType.
Name + " ;_ Proxy" ;theClassBuilder.AppendLine ( " ;using ;System ;" ;) theClassBuilder.AppendLine ( " ;using ;System.Collections.Generic ;" ;theClassBuilder.AppendLine ) ;( " ;using ;System.
Text ;" ;theClassBuilder.AppendLine ( " ) ;using System.Reflection ;" ;theClassBuilder.AppendLine ) ;( " ;using ;System.Reflection.Emit ;" ;theClassBuilder.AppendLine ( " ) ;namespace " ;theTargetType.
Namespace theClassBuilder.AppendLine ( ) ;" ;{" ;) ( " ;theClassBuilder.AppendLine ;public class " ;theProxyClassName+" :" ;theTargetType.FullName ;" ;theClassBuil {" ;) ;Der.
AppendLine ( " ;private " ;+ theTargetType.FullName + " ;_ target ;" ;theClassBuilder.AppendLine ) ;( " ;private List< ;IMethodInterceptor> ;adviceList = null _ ;" ;" ;public ;theClassBuilder.
AppendLine ) ( " _ _ ;+ theTargetType.Name + " ;_ ( " ;Proxy + theTargetType.FullName + " ;target ,List< ;IMethodInterceptor> ;" ;AdviceList ) ) ;theClassBuilder.
AppendLine ( " ;{" ;) ;theClassBuilder.AppendLine ( " ;target = target " _ ;;) ;theClassBuilder.AppendLine ( " ;this._adviceList = AdviceList ;" ;theClassBuilder.
AppendLine ( ) ;" " ;} ;/ / get ) ;target class methods ,ready to give the proxy class the inheritance of MethodInfo methodinfos = theTargetType.GetMethods ( BindingFlags.Public BindingFlags.
InvokeMethod BindingFlags.Instance ) ;foreach ( MethodInfo MI in methodinfos ) {if ( mi.IsVirtual ) {string theRetType = mi.ReturnType.FullName ;if ( mi.ReturnType = = typeof ( void ) ) {theRetType = " ;void" ;} ;string theParams = " ;" ;string ;theTypes = " ;" ;string ;thePMInsts = " ;" ;ParameterInfo thePs = ;mi.
GetParameters ( ) ;for ( int i = 0 ;I < ;thePs.Length ;I + + ) {if ( I = = 0 ) {theTypes = " ;typeof ( " ;thePs . ParameterType.FullName + " ;" ;if ) ;( thePs .
IsOut ) {theParams = " ;out " ;thePs + ParameterType.FullName + " .;" ;+ thePs . Name ;thePMInsts = " ;out " ;thePs . Name ;} else { TheParams = " ;" ;thePs .
ParameterType.FullName + " ;" ;thePs . Name ;thePMInsts = " ;" ;thePs . Name ;} } else {theTypes = " ;( " ;thePs ,typeof + . ParameterType.FullName + " ;) " ;if ;( thePs .
IsOut ) {thePMInsts = " ;" ,Dr Dre Headphones,out ;thePs . Name ;theParams = " ,out ;" ;thePs . ParameterType.FullName + " ;" ;thePs . Name ;} else {thePMInsts = " ;," ;+ thePs .
Name ;theParams = " ," ;;+ thePs . ParameterType.FullName + " ;" ;thePs . Name ;} } } theClassBuilder.AppendLine ( " ;public override " ;" + theRetType + " + mi.
Name + " ;;;( " ;" + theParams + " ;) ;) ;theClassBuilder.AppendLine ( " ;{" ;) ;theClassBuilder.AppendLine ( " ;if ( _ adviceList ,Nike Running Shoes!= null ) " " theClassBuilder.
AppendLine ( ;;) ;{" ;) ;theClassBuilder.AppendLine ( " ;foreach ( IMethodInterceptor item in adviceList " _ ) ;) ;theClassBuilder.AppendLine ( " ;{" ;) ;theClassBuilder.
AppendLine ( " ;MethodInfo = theMI target.GetType ( ) . GetMethod ( _ " ;" ;" + mi.Name + " ,new Type ; ; {" ;+ theTypes + " ;} ) ;" ;) ;theClassBuilder.
AppendLine ( " ;AdviseInvocation theInvocation = new AdviseInvocation ( target theMI ,new object _ , {" ;thePMInsts " ;} ) ;" ;( mi.ReturnType ;if ) = = = = = = = = = = = = typeof ( void ) ) {theClassBuilder.
AppendLine ( " ;item.Invoke ;" ;( theInvocation ) ) ;} else {theClassBuilder.AppendLine ( " ;return ( " + theRetType + " ;;) ( item.Invoke ( theInvocation ) ) ;" ;) ;} theClassBuilder.
AppendLine ( " " ;} ;theClassBuilder.AppendLine ( " ;} ) ;" ;) ;if ( mi.ReturnType = = typeof ( void ) ) {theClassBuilder.AppendLine ( " + mi.Name + target.
" ;_ ;( " ;" ;thePMInsts ;" ;" ;) ) ;} else {theClassBuilder.AppendLine ( ( " ;" ;return + theRetType + " ;( target." + mi.Name ) _ ;+ " ;( " ;thePMInsts + + ) ;" ;" ;) ) ;} theClassBuilder.
AppendLine ( " ;} " ;} } ) ;theClassBuilder.AppendLine ( " " ;} ;theClassBuilder.AppendLine ( ) ;" " ;} ;;) / / test was observed by using Console.WriteLine ( theClassBuilder.
ToString ( ) ) ;/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = dynamic compilation proxy class code = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / / create a compiler .
CSharpCodeProvider provider = new CSharpCodeProvider ( ) ;/ / set the compiler parameters .CompilerParameters Paras = new CompilerParameters ( ) ;paras.GenerateExecutable = false ;paras.
GenerateInMemory = true ;/ / paras.ReferencedAssemblies.Add ( " ;System.dll" ;paras.ReferencedAssemblies.Add ) ;( " ;SpringNetStudy.exe" ;) ;/ / paras.ReferencedAssemblies.
Add ( " ;mscorlib.dll" ;) ;/ / paras.ReferencedAssemblies.Add ( " ;" ;) ;/ / compiler code .CompilerResults result = provider.CompileAssemblyFromSource ( Paras ,theClassBuilder.
ToString ( ) ) ;/ / get the compiled assembly .Assembly assembly = result.CompiledAssembly ;Type theType = assembly.GetType ( theTargetType.Namespace+" ;" ;theProxyClassName ,false .
,true ) ;VAR obj = Activator.CreateInstance ( theType ,new object { _ target ,_ adviceList} ) ;return obj ;} } postscript: some things seem to be very mysterious ,but in fact is just so so .
China can not make a good frame ,in fact, one of the main reasons is the compiler technology does not pass, do not have their own language .If the Emit and dynamic compilation namespace method does not open ,it .
Related articles:

没有评论:

发表评论