mh_frame_sps/APT.Infrastructure.Core/Helper/ReflectHelper.cs

122 lines
3.8 KiB
C#
Raw Normal View History

2026-04-07 13:47:52 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace APT.Infrastructure.Core.Refctor
{
public class FuctionAttrDesc
{
public Guid ID { get; set; }
public string FuncName { get; set; }
public string AttrName { get; set; }
public List<ParmaterModel> Parmaters { get; set; }
}
public class ParmaterModel
{
public Guid ID { get; set; }
public string ParmaterName { get; set; }
public string ParmaterType { get; set; }
public string ParmaterAttr { get; set; }
}
public static class ReflectHelper
{
public static Type FindTypeInCurrentDomain(string typeName)
{
Type type = null;
//如果该类型已经装载
type = Type.GetType(typeName);
if (type != null)
{
return type;
}
//在EntryAssembly中查找
if (Assembly.GetEntryAssembly() != null)
{
type = Assembly.GetEntryAssembly().GetType(typeName);
if (type != null)
{
return type;
}
}
//在CurrentDomain的所有Assembly中查找
Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
int assemblyArrayLength = assemblyArray.Length;
for (int i = 0; i < assemblyArrayLength; ++i)
{
type = assemblyArray[i].GetType(typeName);
if (type != null)
{
return type;
}
}
for (int i = 0; (i < assemblyArrayLength); ++i)
{
Type[] typeArray = assemblyArray[i].GetTypes();
int typeArrayLength = typeArray.Length;
for (int j = 0; j < typeArrayLength; ++j)
{
if (typeArray[j].Name.Equals(typeName))
{
return typeArray[j];
}
}
}
return type;
}
public static List<FuctionAttrDesc> GetClassMethodAttr(Type type)
{
List<FuctionAttrDesc> fads = new List<FuctionAttrDesc>();
MethodInfo[] properties = type.GetMethods();
if (properties.Any())
{
foreach (var p in properties)
{
object[] attrs = p.GetCustomAttributes(typeof(FuctionDescAttribute), true);
if (attrs.Length == 1)
{
var param = p.GetParameters();
var s = (FuctionDescAttribute)attrs[0];
FuctionAttrDesc fad = new FuctionAttrDesc();
fad.AttrName = s.FucName;
fad.FuncName = p.Name;
fad.Parmaters = new List<ParmaterModel>();
if (param.Length != s.ParamterDesc.Length)
{
//throw new Exception("参数个数不匹配");
}
for (var n = 0; n < param.Length; n++)
{
ParmaterModel mode = new ParmaterModel();
mode.ParmaterName = param[n].Name;
mode.ParmaterType = param[n].ParameterType.Name;
if (s.ParamterDesc.Length >= n + 1)
{
mode.ParmaterAttr = s.ParamterDesc[n];
}
fad.Parmaters.Add(mode);
}
fads.Add(fad);
}
}
}
return fads;
}
}
}