WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

反射的使用

Posted on   WebEnh  阅读(31)  评论(0编辑  收藏  举报

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

 

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多