参考 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(); }