WEB开发网
开发学院软件开发C语言 关于C#中动态加载AppDomain的问题 阅读

关于C#中动态加载AppDomain的问题

 2009-06-24 07:08:14 来源:WEB开发网   
核心提示: 2、创建RemoteLoader类,它可以在AppDomain中自由穿越,关于C#中动态加载AppDomain的问题(3),这就需要继承System.MarshalByRefObject这个抽象类,这里RemoteLoader如果不继承MarshalByRefObject类则一定会报错(在

2、创建RemoteLoader类,它可以在AppDomain中自由穿越,这就需要继承System.MarshalByRefObject这个抽象类,这里RemoteLoader如果不继承MarshalByRefObject类则一定会报错(在不同AppDomain间传递对象,该对象必须是可序列化的)。以RemoteLoader类做为代理来调用待执行的.Net程序。

using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Text;

    /// <summary>
    /// The Remote loader.
    /// </summary>
    public class RemoteLoader : MarshalByRefObject
    {
        /// <summary>
        /// The assembly we need.
        /// </summary>
        private Assembly assembly = null;

        /// <summary>
        /// The output.
        /// </summary>
        private String output = String.Empty;

        /// <summary>
        /// Gets the output.
        /// </summary>
        /// <value>The output.</value>
        public String Output
        {
            get
            {
                return this.output;
            }
        }

        /// <summary>
        /// Invokes the method.
        /// </summary>
        /// <param name="fullName">The full name.</param>
        /// <param name="className">Name of the class.</param>
        /// <param name="argsInput">The args input.</param>
        /// <param name="programName">Name of the program.</param>
        public void InvokeMethod(String fullName, String className, String argsInput, String programName)
        {
            this.assembly = null;
            this.output = String.Empty;

            try
            {
                this.assembly = Assembly.LoadFrom(fullName);

                Type pgmType = null;
                if (this.assembly != null)
                {
                    pgmType = this.assembly.GetType(className, true, true);
                }
                else
                {
                    pgmType = Type.GetType(className, true, true);
                }

                Object[] args = RunJob.GetArgs(argsInput);

                BindingFlags defaultBinding = BindingFlags.DeclaredOnly | BindingFlags.Public
                        | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase
                        | BindingFlags.InvokeMethod | BindingFlags.Static;

                CultureInfo cultureInfo = new CultureInfo("es-ES", false);

                try
                {
                    MethodInfo methisInfo = RunJob.GetItsMethodInfo(pgmType, defaultBinding, programName);
                    if (methisInfo == null)
                    {
                        this.output = "EMethod does not exist!";
                    }

                    if (methisInfo.IsStatic)
                    {
                        if (methisInfo.GetParameters().Length == 0)
                        {
                            if (methisInfo.ReturnType == typeof(void))
                            {
                                pgmType.InvokeMember(programName, defaultBinding, null, null, null, cultureInfo);
                                this.output = "STo call a method without return value successful.";
                            }
                            else
                            {
                                this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, null, null, cultureInfo);
                            }
                        }
                        else
                        {
                            if (methisInfo.ReturnType == typeof(void))
                            {
                                pgmType.InvokeMember(programName, defaultBinding, null, null, args, cultureInfo);
                                this.output = "STo call a method without return value successful.";
                            }
                            else
                            {
                                this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, null, args, cultureInfo);
                            }
                        }
                    }
                    else
                    {
                        if (methisInfo.GetParameters().Length == 0)
                        {
                            object pgmClass = Activator.CreateInstance(pgmType);

                            if (methisInfo.ReturnType == typeof(void))
                            {
                                pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, null, cultureInfo);
                                this.output = "STo call a method without return value successful.";
                            }
                            else
                            {
                                this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, null, cultureInfo);   //'ymtpgm' is program's name and the return value of it must be started with 'O'.
                            }
                        }
                        else
                        {
                            object pgmClass = Activator.CreateInstance(pgmType);

                            if (methisInfo.ReturnType == typeof(void))
                            {
                                pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, args, cultureInfo);
                                this.output = "STo call a method without return value successful.";
                            }
                            else
                            {
                                this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, pgmClass, args, cultureInfo);   //'ymtpgm' is program's name and the return value of it must be started with 'O'.
                            }
                        }
                    }
                }
                catch
                {
                    this.output = (String)pgmType.InvokeMember(programName, defaultBinding, null, null, null, cultureInfo);
                }
            }
            catch (Exception e)
            {
                this.output = "E" + e.Message;
            }
        }


    } 

   其中的InvokeMethod方法只要提供Assembly的全名、类的全名、待执行方法的输入参数和其全名就可以执行该方法,该方法可以是带参数或不带参数,静态的或者不是静态的。

   最后这样使用这两个类:

AssemblyDynamicLoader loader = new AssemblyDynamicLoader();
String output = loader.InvokeMethod("fileName", "ymtcla", "yjoinp", "ymtpgm");


 loader.Unload(); 

上一页  1 2 3 

Tags:关于 动态 加载

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接