熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

詳解.NET中的動態編譯技術[3]

2022-06-13   來源: .NET編程 

  · 創建另外一個Application Domain

  · 動態創建(編譯)代碼並保存到磁盤

  · 創建一個公共的遠程調用接口

  · 創建遠程調用接口的實例並通過這個接口來訪問其方法

  換句話來講就是將對象加載到另外一個AppDomain中並通過遠程調用的方法來調用所謂遠程調用其實也就是跨應用程序域調用所以這個對象(動態代碼)必須繼承於MarshalByRefObject類為了復用這個接口被單獨提到一個工程中並提供一個工廠來簡化每次的調用操作

using System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemText;

using SystemReflection;

namespace RemoteAccess

{

    ///

          /// Interface that can be run over the remote AppDomain boundary

          ///

          public interface IRemoteInterface

          {

                   object Invoke(string lcMethodobject[] Parameters);

          }

 

          ///

          /// Factory class to create objects exposing IRemoteInterface

          ///

          public class RemoteLoaderFactory : MarshalByRefObject

          {

                   private const BindingFlags bfi = BindingFlags        Instance | BindingFlagsPublic           | BindingFlagsCreateInstance;

 

                   public RemoteLoaderFactory() {}

 

        public IRemoteInterface Create( string assemblyFile string typeName         object[] constructArgs )

                   {

                 return (IRemoteInterface) ActivatorCreateInstanceFrom(

                 assemblyFile typeName false bfi null constructArgs

                                   null null null )Unwrap();

                   }

          }       

}

[]  []  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/14506.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.