C#相关 获取类中 变量名字 函数名字 属性名字 IsAssignableFrom
参考 https://blog.csdn.net/xiaolei1982/article/details/2294364
获取类外层的标签:
[DataContract1("People")] public class Person { public String UserName { get; set; } public String UserAge { get; set; } static void Main(string[] args) { //反射自己这类 Person aa = new Person(); Type aaacc = typeof(Person); var attribute = aaacc.GetCustomAttribute(typeof(DataContract1Attribute)); Console.WriteLine(""); } } [AttributeUsage(AttributeTargets.All)] public class DataContract1Attribute : Attribute { public string Name; public DataContract1Attribute(string Name) { this.Name = Name; } }
Type a = typeof(A); PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); foreach (PropertyInfo pi in piArr) { Console.Write(pi.Name); //显示所有的属性 } Console.WriteLine(""); MethodInfo[] info = a.GetMethods(); foreach (MethodInfo pi in info) { Console.Write(pi.Name+" "); } Console.WriteLine(""); FieldInfo[] inMemberInfofo = a.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); A aaaa = new A(); foreach (FieldInfo pi in inMemberInfofo) { Console.Write(pi.Name+" "); Console.Write(" 值:" + pi.GetValue(aaaa)); Console.Write(" 类型:" + pi.FieldType.Equals(typeof(int))); } Console.WriteLine(""); string aaa = "Ab"; Console.WriteLine(aaa.IndexOf("A", StringComparison.OrdinalIgnoreCase));
https://www.cnblogs.com/tomorrow0/p/14381500.html
IsAssignableFrom
bool res = {TypeA}.IsAssignableFrom({TypeB}) ;
如果TypeA和TypeB类型一样则返回true;
如果TypeA是TypeB的父类则返回true;
如果TypeB实现了接口TypeA则返回true;
Console.WriteLine("--"); Type a = typeof(A); Console.WriteLine(a.AssemblyQualifiedName); Console.WriteLine("--"); Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies(); for (int i = 0; i != assemblies.Length; ++i) { Type type = typeof(Program).Assembly.GetType("Program+A");//查看 AssemblyQualifiedName的输出,每个C#版本可能不一致 if (type != null) { Console.WriteLine("不区分大小写"); break; } }
Type type = typeof(Flux.FObject).Assembly.GetType("Flux." + className);
private void ShowAddTrackMenu() { Event.current.Use(); GenericMenu menu = new GenericMenu(); System.Reflection.Assembly fluxAssembly = typeof(FEvent).Assembly; Type[] types = typeof(FEvent).Assembly.GetTypes(); //程序集合的所有类型信息 if( fluxAssembly.GetName().Name != "Assembly-CSharp" ) { // if we are in the flux trial, basically allow to get the types in the project assembly ArrayUtility.AddRange<Type>( ref types, System.Reflection.Assembly.Load("Assembly-CSharp").GetTypes() ); } List<KeyValuePair<Type, FEventAttribute>> validTypeList = new List<KeyValuePair<Type, FEventAttribute>>(); foreach( Type t in types ) { if( !typeof(FEvent).IsAssignableFrom( t ) ) //查看是否FEvent相关的类型 continue; object[] attributes = t.GetCustomAttributes(typeof(FEventAttribute), false); if( attributes.Length == 0 || ((FEventAttribute)attributes[0]).menu == null ) continue; validTypeList.Add( new KeyValuePair<Type, FEventAttribute>(t, (FEventAttribute)attributes[0]) ); } validTypeList.Sort( delegate(KeyValuePair<Type, FEventAttribute> x, KeyValuePair<Type, FEventAttribute> y) { return x.Value.menu.CompareTo( y.Value.menu ); }); foreach( KeyValuePair<Type, FEventAttribute> kvp in validTypeList ) { menu.AddItem( new GUIContent(kvp.Value.menu), false, AddTrackMenu, kvp ); } menu.ShowAsContext(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!