C#反射
2017-05-09 22:36:32 27 举报
AI智能生成
C# 反射
作者其他创作
大纲/内容
反射相关类
System.Reflection.Assembly
反射单个程序集
ReflectionOnlyLoad
LoadFrom(assemblyName)
LoadFile(assemblyPath)
Load
常用属性
DefinedTypes 获取定义在此程序集中的类型的集合。
FullName 获取程序集的显示名称。
IsDynamic 当前程序集是否是通过使用反射发出在当前进程中动态生成的
常用方法
CreateInstance(String)
CreateInstance(String, Boolean)
CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])
GetAssembly(Type)
GetCallingAssembly
GetName()
GetCustomAttributes(Boolean)
GetCustomAttributes(Type, Boolean)
GetExecutingAssembly()
GetTypes()
- GetType(String)
- GetType(String, Boolean)
- GetType(String, Boolean, Boolean)
IsDefined(Type, Boolean)
CustomAttributeExtensions(扩展方法)
- GetCustomAttribute(Type)
- GetCustomAttribute<T>()
GetCustomAttributes(Type)
GetCustomAttributes()
IsDefined(Type)
System.Reflection.MemberInfo
属性
CustomAttributes
DeclaringType
MemberType
All
Constructor
Custom
Event
Field
Method
NestedType
Property
TypeInfo
Module
Name
ReflectedType
方法
GetType()
System.Reflection.EventInfo
属性
DeclaringType
Name
ReflectedType
RaiseMethod
RemoveMethod
AddMethod
System.Reflection.FieldInfo
属性
FieldType
IsAssembly
IsFamily
IsFamilyAndAssembly
IsFamilyOrAssembly
IsInitOnly
IsLiteral
IsNotSerialized
IsPrivate
IsPublic
IsStatic
方法
GetValue(Object)
SetValue(Object, Object)
SetValue(Object, Object, BindingFlags, Binder, CultureInfo)
System.Reflection.MethodBase
属性
IsAbstract
IsAssembly
IsConstructor
IsFamily
IsFamilyAndAssembly
IsFamilyOrAssembly
IsFinal
IsPrivate
IsPublic
IsGenericMethod
IsGenericMethodDefinition
IsStatic
IsVirtual
ContainsGenericParameters
方法
GetGenericArguments()
GetParameters()
GetCurrentMethod()
Invoke(Object, Object[])
Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)
System.Reflection.MethodInfo
属性
ReturnParameter
ReturnType
ReturnTypeCustomAttributes
System.Reflection.ConstructorInfo
方法
Invoke(BindingFlags, Binder, Object[], CultureInfo)
Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)
Invoke(Object, Object[])
Invoke(Object[])
System.Reflection.PropertyInfo
属性
CanRead
CanWrite
DeclaringType
GetMethod
SetMethod
Attributes
CustomAttributes
DeclaringType
GetMethod
PropertyType
ReflectedType
方法
GetAccessors
GetConstantValue()
GetGetMethod()
GetSetMethod()
GetType()
- GetValue(Object)
- GetValue(Object, BindingFlags, Binder, Object[], CultureInfo)
- GetValue(Object, Object[])
- SetValue(Object, Object)
- SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo)
- SetValue(Object, Object, Object[])
System.Type
属性
Name 数据类型名
FullName 数据类型的完全限定名(包括命名空间名)
Namespace 定义数据类型的命名空间名
IsAbstract 指示该类型是否是抽象类型
IsArray 指示该类型是否是数组
IsClass 指示该类型是否是类
IsEnum 指示该类型是否是枚举
IsInterface 指示该类型是否是接口
IsPublic 指示该类型是否是公有的
IsSealed 指示该类型是否是密封类
IsValueType 指示该类型是否是值类型
BaseType
ContainsGenericParameters
DeclaringMethod
DeclaringType
GenericParameterAttributes
GenericParameterPosition
GenericTypeArguments
HasElementType
IsAnsiClass
IsAutoClass
IsByRef
IsCOMObject
IsConstructedGenericType
IsGenericType
IsGenericTypeDefinition
IsImport
IsMarshalByRef
IsNestedAssembly
IsNestedFamANDAssem
IsNestedFamily
IsNestedFamORAssem
IsNestedPrivate
IsNestedPublic
IsNotPublic
IsPointer
IsPrimitive
IsSerializable
IsUnicodeClass
IsValueType
IsVisible
MemberType
所有方法
FindInterfaces(TypeFilter, Object)
FindMembers(MemberTypes, BindingFlags, MemberFilter, Object)
GetArrayRank()
GetConstructor
GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])
GetConstructor(Type[])
GetConstructors()
GetConstructors(BindingFlags)
GetElementType
Enum相关
GetEnumName(Object)
GetEnumNames()
GetEnumUnderlyingType()
GetEnumValues()
IsEnumDefined(Object)
Event相关
GetEvent(String, BindingFlags)
GetEvents()
GetEvents(BindingFlags)
GetEvents(BindingFlags)
Field相关
GetField(String)
GetField(String, BindingFlags)
GetFields()
GetFields(BindingFlags)
Generic相关
GetGenericArguments()
GetGenericParameterConstraints()
GetGenericTypeDefinition()
Interface相关
GetInterface(String)
GetInterface(String, Boolean)
GetInterfaceMap(Type)
GetInterfaces()
Member相关
GetMember(String)
GetMember(String, BindingFlags)
GetMember(String, MemberTypes, BindingFlags)
GetMembers()
GetMembers(BindingFlags)
Method相关
GetMethod(String)
GetMethod(String, BindingFlags)
GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])
GetMethod(String, Type[])
GetMethod(String, Type[], ParameterModifier[])
GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
GetMethods()
GetMethods(BindingFlags)
NestedType相关
GetNestedType(String)
GetNestedType(String, BindingFlags)
GetNestedTypes()
GetNestedTypes(BindingFlags)
Property相关
GetProperties()
GetProperties(BindingFlags)
GetProperty(String)
GetProperty(String, BindingFlags)
GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
GetProperty(String, Type)
GetProperty(String, Type, Type[])
GetProperty(String, Type, Type[], ParameterModifier[])
GetProperty(String, Type[])
GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
GetType相关
GetType(String)
GetType(String, Boolean)
GetType(String, Boolean, Boolean)
GetType(String, Func, Func)
GetType(String, Func, Func, Boolean)
GetType(String, Func, Func, Boolean, Boolean)
InvokeMember
nvokeMember(String, BindingFlags, Binder, Object, Object[])
InvokeMember(String, BindingFlags, Binder, Object, Object[], CultureInfo)
InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])
IsArrayImpl()
IsByRefImpl()
继承关系判断
IsAssignableFrom(Type)
IsInstanceOfType(Object)
IsSubclassOf(Type)
MakeArrayType()
MakeArrayType(Int32)
MakeByRefType()
MakeGenericType(Type[])
ReflectionOnlyGetType(String, Boolean, Boolean)
常用方法
GetConstructor(), GetConstructors():返回ConstructorInfo类型,用于取得该类的构造函数的信息
GetEvent(), GetEvents():返回EventInfo类型,用于取得该类的事件的信息
GetField(), GetFields():返回FieldInfo类型,用于取得该类的字段(成员变量)的信息
GetInterface(), GetInterfaces():返回InterfaceInfo类型,用于取得该类实现的接口的信息
GetMember(), GetMembers():返回MemberInfo类型,用于取得该类的所有成员的信息
GetMethod(), GetMethods():返回MethodInfo类型,用于取得该类的方法的信息
GetProperty(), GetProperties():返回PropertyInfo类型,用于取得该类的属性的信息
获取Type方法
Type t = typeof(string);
使用对象GetType()方法。
string s = "grayworm";
Type t = s.GetType();
string s = "grayworm";
Type t = s.GetType();
Type t = Type.GetType("System.String");
System.Reflection.AppDomain
System.Reflection.Module
反射创建实例方法
System.Activator
Activator.CreateInstance (Type)
Activator.CreateInstance (Type, Object[])
Activator.CreateInstance (Type, Object[])
Activator.CreateInstanceFrom(assemblyPath,type)
Activator.CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm)
System.AppDomain
CreateInstance
CreateInstanceAndUnwrap
CreateInstanceFrom
CreateInstanceFromAndUnwrap
System.Type
Type.InvokerMember
System.Reflection.ConstructorInfo
ConstructorInfo.Invoke
数组,委托和泛型类型的创建
Array.CreateInstance:var array = Array.CreateInstance(typeof(int),20);
Delegate.CreateDelegate
Generic
Type open = typeof(Dictionary<,>);
Type open = typeof(Dictionary<,>);
Type closeType = open.MakeGenericType(typeof(String),typeof(object));
object obj = Activator.CreateInstance(closeType);
Console.WriteLine(obj.GetType());
运算符
is
子主题
BindingFlags
CreateInstance
DeclaredOnly
Default
ExactBinding
FlattenHierarchy
GetField
GetField
IgnoreCase
IgnoreReturn
Instance
InvokeMethod
NonPublic
OptionalParamBinding
Public
PutDispProperty
PutRefDispProperty
SetField
SetProperty
Static
SuppressChangeType
TypeFilter
基元类型
CallingConventions
Any
ExplicitThis
HasThis
Standard
VarArgs
泛型类型定义
0 条评论
下一页