116 lines
5.5 KiB
C#
116 lines
5.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Windows.Forms;
|
||
|
||
|
||
namespace DrawGraph
|
||
{
|
||
public class BoardFormUtil
|
||
{
|
||
|
||
public static AreaManageBase AreaManageFactory(string instanceName, object parameter, ZedGraphControl parameter1, TemplateManage parameter2, string parameter3)
|
||
{
|
||
AreaManageBase reValue = null;
|
||
try
|
||
{
|
||
Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集
|
||
//object obj = assembly.CreateInstance("ReflectionTest.Test"); //类的完全限定名(即包括命名空间)
|
||
object[] parameters = new object[4];
|
||
parameters[0] = parameter;
|
||
parameters[1] = parameter1;
|
||
parameters[2] = parameter2;
|
||
parameters[3] = parameter3;
|
||
reValue = (AreaManageBase)assembly.CreateInstance("DrawGraph." + instanceName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null);// 创建类的实例
|
||
if(reValue == null)
|
||
reValue = (AreaManageBase)assembly.CreateInstance("DrawGraph.document." + instanceName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null);
|
||
if (reValue != null) reValue.ClassName = instanceName;
|
||
}
|
||
catch(Exception )
|
||
{
|
||
}
|
||
return reValue;
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="dllName">DLL文件名,必须在当前运行项目的目录里</param>
|
||
/// <param name="nameSpace">命名空间名</param>
|
||
/// <param name="instanceName">类名</param>
|
||
/// <returns></returns>
|
||
public static T GetObjectFactory<T>(string dllName,string nameSpace,string instanceName){
|
||
|
||
T reValue;
|
||
try
|
||
{
|
||
Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + dllName);//你的loadfile
|
||
//Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集
|
||
//object obj = assembly.CreateInstance("ReflectionTest.Test"); //类的完全限定名(即包括命名空间)
|
||
reValue = (T)assembly.CreateInstance(nameSpace + instanceName, true, System.Reflection.BindingFlags.Default, null, null, null, null);// 创建类的实例
|
||
}
|
||
catch
|
||
{
|
||
reValue = default;
|
||
}
|
||
return reValue;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取对象的所有属性列表
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
/// <returns></returns>
|
||
public static List<PropertyObject> GetPropertyList(Object obj, TemplateManage template = null)
|
||
{
|
||
List<PropertyObject> propertyList = new List<PropertyObject>();
|
||
if (obj != null)
|
||
{
|
||
Type type = obj.GetType();
|
||
PropertyInfo[] infos = type.GetProperties();
|
||
foreach (PropertyInfo info in infos)
|
||
{
|
||
try {
|
||
Attribute[] attribs = Attribute.GetCustomAttributes(info);
|
||
Attribute attrib = attribs.FirstOrDefault<Attribute>(s => s is NoCreatControlAttributs);
|
||
//如果增加了NoCreatControlAttributs这个属性的属性则不生成列表
|
||
if (attrib != null) continue;
|
||
PropertyObject proObj = new PropertyObject();
|
||
proObj.Key = info.Name;
|
||
Attribute attDesc = attribs.FirstOrDefault<Attribute>(s => s is ClassAttributs);
|
||
if (attDesc != null)
|
||
{
|
||
ClassAttributs cattr = attDesc as ClassAttributs;
|
||
if (cattr != null)
|
||
{
|
||
proObj.Description = cattr.Description;
|
||
}
|
||
}
|
||
//如果不是值类型,且没标明ClassAttributs属性标签的引用类型,就继续往下循环
|
||
if (info.PropertyType != typeof(Color) && info.PropertyType != typeof(int) && info.PropertyType != typeof(string) && info.PropertyType != typeof(DateTime) && info.PropertyType != typeof(Decimal) && info.PropertyType != typeof(bool) && info.PropertyType != typeof(Nullable<int>) && info.PropertyType != typeof(Nullable<DateTime>) && info.PropertyType != typeof(Nullable<Decimal>) && info.PropertyType != typeof(Nullable<bool>))
|
||
{
|
||
proObj.SubPropertyList = GetPropertyList(info.GetValue(obj, null), template);
|
||
}
|
||
else
|
||
{
|
||
object val = info.GetValue(obj, BindingFlags.GetProperty, null, null, null);
|
||
proObj.Value = (val != null) ? val.ToString() : "";
|
||
proObj.Text = proObj.Value;
|
||
}
|
||
propertyList.Add(proObj);
|
||
} catch (Exception exp) {
|
||
MessageBox.Show(exp.Message.ToString());
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//Console.WriteLine(obj.ToString());
|
||
}
|
||
return propertyList;
|
||
}
|
||
}
|
||
}
|