首页 > 其他分享 >反射的使用

反射的使用

时间:2022-12-02 22:35:26浏览次数:32  
标签:control 反射 Assembly String System 使用 assembly null

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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、获取接口:

1 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, nullnullnullnullas IUIC;

3、获取xml文件

1 2 3 4 5 6 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、创建窗体:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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, nullnullnullnullas Form;            }            else            {                form = formAssembly.CreateInstance(formFullName, false, BindingFlags.Default, null, formArgs, nullnullas Form;            }              if (form == null)            {                string strError = string.Format("CreateForm失败\nformAssemblyFile={0}\nformFullName={1}\nformName={2}\nformText={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;

 

 

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

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #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

 

标签:control,反射,Assembly,String,System,使用,assembly,null
From: https://www.cnblogs.com/webenh/p/16945841.html

相关文章

  • ForexClub:如何在外汇交易中使用心理水平
    在下面的澳元/日元图表中,75.00的价格水平出现了六次强烈的拐点。每次价格接近75.00时,货币对都会反弹。这是因为交易员看到75.00的价格并认为这是便宜的,这促使澳......
  • 1-对于pip的使用
    1-python是一款功能非常丰富的软件,pip有助于我们去发现那些丰富的功能2-使用pip去管理python包2.1pipinstall<包名>安装指定的包pipun......
  • 使用 cookies
    访问的页面需要cookies时,在脚本中加入“配置元件”­>“HTTPCookies管理器”,要点:cookies管理器元件需要位于需要使用cookies的请求的上一级节点。 在示例中加......
  • NUXT 2.0 使用 swiper
    NUXT2.0使用swiper版本(swiper高版本有改变)"dependencies":{"nuxt":"^2.0.0","swiper":"^4.5.1","vue-awesome-swiper":"^4.1.1"},插件使......
  • SSM使用ajax实现图片上传与删除功能
    图片上传与删除​​1.上传文件​​​​2.删除数据,并且删除对应的文件​​​​3.修改上传文件至非项目路径​​之前写了一篇博客记录了关于修改资料中的图片上传​​(传送门......
  • 【RSA加密】初探RSA并简单使用
    RSA简介,这里贴上一篇博客,讲的很详细,通俗易懂初步理解之后,下面是关于RSA的简单使用:这里贴上一篇优秀的前端加密,后端解密的博客,简单使用的话是可以了。看完上面两篇博客,就够用......
  • 【序列化】Java中将使用PHP序列化工具将数据序列化
    在项目中需要和PHP公用一个MySQL数据库,有些数据需要序列化之后保存,这就需要将待存储的数据序列化之后存到数据库中,取出的时候,需要反序列化之后才能正常使用。原数据:{"06008......
  • 阿里云的堡垒机无法使用秘钥登录Ubuntu服务器
    服务器是阿里云上的Ubuntu22.04堡垒机是用的阿里云提供的秘钥对也是阿里云生成的但是,堡垒机始终无法用秘钥登录该服务器,只能使用密码登录查看日志/var/log/auth.log,有如下......
  • 【坑】springboot使用Junit进行单元测试报错
    在使用myeclipse写了一个简单的springboot的demo进行进行Junit测试的时候,发现一直是这个错误,刚开始遇到这个问题,还以为是别人博客里面写的不够清晰,哪里没有注意到的问题,结果......
  • Integer的使用小计
    关于Integer的范围,可能这个大家都接触过,Java基础面试题中,经常出现的一个问题就是int的范围。但是离了面试题之后,我们是否还能注意到这个范围呢?最近在做项目的时候发现了一颗......