zoukankan      html  css  js  c++  java
  • 反射的使用

    1.将主程序界面上的Icon赋给基类内的Icon(同时其他子类也具有了此Icon):

        public partial class BaseForm : Form
        {
            public BaseForm()
            {
                InitializeComponent();
    
                String exeFileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\A.B.Main.exe";
                if (System.IO.File.Exists(exeFileName))
                {
                    System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(exeFileName);
                    if (assembly != null)
                    {
                        System.IO.Stream stream = assembly.GetManifestResourceStream("A.B.Main.C.ico");
                        if (stream != null)
                        {
                            this.Icon = new Icon(stream);
                        }
                    }
                }
            }
        }
    

      

     2、获取接口:

     Assembly assembly = Assembly.LoadFrom(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\A.B.UI.dll");
                this.iUIC = assembly.CreateInstance("A.B.UI.UIC", false, BindingFlags.Default, null, null, null, null) as IUIC;
    

    3、获取xml文件

     Assembly assembly = Assembly.LoadFrom(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\A.B.UI.dll");
                Stream stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".Config.xml");
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                xmlDoc.Load(stream);
                stream.Close();
                stream.Dispose();
    

    4、创建窗体:

     public static Form CreateForm(String formAssemblyFile, String formFullName, Object[] formArgs, String formName, String formText)
            {
                Form form;
                Assembly formAssembly = Assembly.LoadFrom(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\" + formAssemblyFile);
    
                if (formArgs == null)
                {
                    form = formAssembly.CreateInstance(formFullName, false, BindingFlags.Default, null, null, null, null) as Form;
                }
                else
                {
                    form = formAssembly.CreateInstance(formFullName, false, BindingFlags.Default, null, formArgs, null, null) as Form;
                }
    
    			if (form == null)
    			{
    				string strError = string.Format("CreateForm失败
    formAssemblyFile={0}
    formFullName={1}
    formName={2}
    formText={3}",
    					formAssemblyFile,
    					formFullName,
    					formName,
    					formText);
    				throw new Exception(strError);
    			}
    
                if (!String.IsNullOrEmpty(formName))
                {
                    form.Name = formName;
                }
                if (!String.IsNullOrEmpty(formText))
                {
                    form.Text = formText;
                }
    
                return form;
            }
    

     4、获取版本信息:

    (1)Assembly.GetExecutingAssembly().GetName().Version.ToString();

    (2)object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                    if (attributes.Length == 0)
                    {
                        return "";
                    }
                    return ((AssemblyDescriptionAttribute)attributes[0]).Description;

    (3)object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                    if (attributes.Length == 0)
                    {
                        return "";
                    }
                    return ((AssemblyProductAttribute)attributes[0]).Product;

       反射对控件的操作:调用函数(含参数|不含参数)

         #region 调用控件方法
            private void InvokeMethod(String methodName, Control control, Object[] args)
            {
                this.SetPropertyValue("UserTempFilePath", control, this.userTempPathFullName);
    
                try
                {
                    Type ctlType = control.GetType();
                    MethodInfo mi = null;
                    if (args == null)
                    {
                        mi = ctlType.GetMethod(methodName, System.Type.EmptyTypes);
                    }
                    else
                    {
                        mi = ctlType.GetMethod(methodName);
                    }
    
                    if (mi != null)
                    {
                        mi.Invoke(control, args);
    
                        this.SetPropertyValue("IsReadOnly", control, true);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
    
            #region 设置控件属性
            private void SetPropertyValue(String propertyName, Control control, Object propertyValue)
            {
                Type ctlType = control.GetType();
                PropertyInfo pi = ctlType.GetProperty(propertyName);
                if (pi != null)
                {
                    pi.SetValue(control, propertyValue, null);
                }
            }
            #endregion
    
  • 相关阅读:
    反转链表 16
    CodeForces 701A Cards
    hdu 1087 Super Jumping! Jumping! Jumping!(动态规划)
    hdu 1241 Oil Deposits(水一发,自我的DFS)
    CodeForces 703B(容斥定理)
    poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))
    ACM 马拦过河卒(动态规划)
    hdu 1005 Number Sequence
    51nod 1170 1770 数数字(数学技巧)
    hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  • 原文地址:https://www.cnblogs.com/shenchao/p/3897195.html
Copyright © 2011-2022 走看看