Reflection in .NET Framework - PowerPoint PPT Presentation

About This Presentation
Title:

Reflection in .NET Framework

Description:

?????? ???????? ?? .NET Framework ? Common Language Runtime (CLR) ?????? ???????? ?? ????? C# ... Professional C#, 3rd, Wrox Press, 2004, ISBN 0764557599 ... – PowerPoint PPT presentation

Number of Views:936
Avg rating:3.0/5.0
Slides: 37
Provided by: ivayloh
Category:

less

Transcript and Presenter's Notes

Title: Reflection in .NET Framework


1
(No Transcript)
2
???????????? ?? .NET Framework
http//www.nakov.com/dotnet/
????????? ?? ????????(Reflection)
?????? ???????
????????? ???????????
3
?????????? ??????
  • ?????? ???????? ?? .NET Framework ? Common
    Language Runtime (CLR)
  • ?????? ???????? ?? ????? C
  • ?????? ???????? ?? MSIL

4
??????????
  • ????? ? Global Assembly Cache?
  • ????? ? Reflection?
  • ????????? ?? ?????????
  • ????????? ?????????? ?? ???????
  • ?????????? ?? ????????? ?? ???????
  • ????????? ????????? ?? ???
  • ????????? ?? ?????? ? ??????????? ??
  • ????????? ?? ??????????? ?? ?????
  • ????????? ????????? ?? ??????
  • Reflection Emit

5
????? ? Global Assembly Cache?
  • Global Assembly Cache (GAC) ? ????????? ?????????
    ?? ????????? ?????????
  • ??????????? ? GAC ?? ???????? ?? ???????? ??
    ?????? .NET ?????????? ?? ????????
  • ??????????? ? GAC ???? ????? ???, ????? ??
    ???????????? ????????
  • ?? ????????? ????????? ? GAC ?????, ??? ?? ?
    ????????? ??????????

6
???????????? 1
  • ??????? ?? GAG ???? Windows Explorer ? ????
    Administrative Tools

7
????? ? Reflection?
  • ??????????? ?? ???????? (reflection) ?
    ???????????? ?? ?????????? ?????????? ?? ????????
    ?? ????? ?? ?????????? ?? ??????????
  • ? Reflection .NET ???????????? ?????
  • ?? ???????? ??????????? ?? ?????????
  • ?? ???????? ???????? ? ?????? ???????
  • ????????? ?? ???????? ??????
  • ????????? ?? ???????? ???? ?????????, ?? ??
    ?????????? ? ?? ?? ???????? ???? ????

8
????????? ?? ?????????
  • ????????? ???? System.Reflection. Asembly.Load()
  • ?????? ???? ?????????
  • ????? ?? ?????????
  • ????? ?? ??? AssemblyName ?????? ?????????
  • ????? ??????? ??? ?????????? ???????? (probing) ?
    ??? ?? ?????? ?? ???????
  • ??? ????????? ?? ???? ???????? ???????????
    FileNotFoundException

Assembly.Load("SomeAssembly.dll")
9
????????? ?? ?????????
  • ????????? ???? System.Reflection.
    Asembly.LoadFrom()
  • ?????? ???? ????????? ???? ?? ?????????
  • ??????? ????????? ????
  • ??????? ???????? Load()
  • ??-????? ?? Load()
  • ??? ????????? ?? ???? ???????? ?? ??????
    FileNotFoundException

Assembly.LoadFrom(_at_"C\Tools\MyAss.dll")
10
????????? ?????????? ?? ???????
  • ???????? ?? System.Reflection. Assembly ??
    ????????? ?????????? ?? ???????
  • FullName
  • ??????? ??? ?? ?????????, ????????? ??????,
    ??????? ? ???? (Public Key Token)
  • Location
  • EntryPoint
  • GlobalAssemblyCache

11
????????? ?????????? ?? ???????
  • ???????? ?? System.Reflection. Assembly ??
    ????????? ?????????? ?? ???????
  • FullName
  • Location
  • ?????, ?? ?????? ? ???????? ?????????
  • EntryPoint
  • GlobalAssemblyCache

12
????????? ?????????? ?? ???????
  • ???????? ?? System.Reflection. Assembly ??
    ????????? ?????????? ?? ???????
  • FullName
  • Location
  • EntryPoint
  • ???????, ?? ????? ?? ??????? ???????????? ??
    ?????????
  • GlobalAssemblyCache

13
????????? ?????????? ?? ???????
  • ???????? ?? System.Reflection. Assembly ??
    ????????? ?????????? ?? ???????
  • FullName
  • Location
  • EntryPoint
  • GlobalAssemblyCache
  • ?????? ????????, ????? ??????? ???? ????????? ?
    ???? ???????? ?? GAC

14
???????????? 2
  • ????????? ?? ?????????

15
?????????? ?? ????????? ?? ???????
  • ?? ?? ??????? ???????????? ?? ?? ???????? ????
    ???????
  • ???????? ? ???????????? ?? ?????? ????????? ?
    ????? ??????
  • ?? ?? ?????????? ?? ?? ???????? ?????? ????????
    ???????? ?? ??????

16
????????? ?? ???????? ? ???????
  • System.Type ???????? ????? ?? ?????????? ??
    ??????????? ? ?????? ? ??????
  • ???? System.Type ????? ?? ??????? ?????? ???????
    ?? ????? ???
  • ??????
  • ??????
  • ????????
  • ???????
  • ??????? ??????
  • ???? Assembly.GetTypes() ????????? ???????? ??
    ?????? ???????

17
????????? ?? ???????? ? ???????
  • Sytem.Type ???????? ????????? ?? ???????? ?
    ?????? ?? ????????? ???????????? ?? ????? ???
  • ????????
  • ??????

BaseType, Attributes, FullName, IsAbstract,
IsArray, IsByRef, IsClass, IsCOMObject, IsEnum,
IsInterface, IsPublic, IsSealed, IsValueType,
Name,
GetConstructors(), GetEvents(), GetFields(),
GetInterfaces(), GetMembers(), GetMethods(),
GetNestedTypes(), GetProperties(),
InvokeMember(), IsInstanceOfType()
18
????????? ????????? ?? ???
??????? ???????? ???????
?????????? ?????? ?????? ? ?????????
Assembly currAssembly Assembly.GetExecuti
ngAssembly() foreach(Type type in
currAssembly.GetTypes()) foreach(MemberInfo
member in type.GetMembers())
Console.WriteLine(member.MemberType)
Console.WriteLine(member.Name)
GetMembers() ????? ????? ?? ????????? ?? ????
19
???????????? 3
  • ?????????? ?? ???????? ? ???????

20
??????? ?? ???????? ???????
  • ?? ????? ??? ??????? ??? ????????? ????, ????? ??
    ??????

21
????????? ?? ?????? ? ??????????? ??
  • Type.GetMethod() ????? ??????????? ?? ?????
    ????? (MethodInfo)
  • MethodInfo.GetParameters() ??????? ???????????
    ?? ????? ?????

MethodInfo someMethod myType.GetMethod("SomeMet
hod") foreach(ParameterInfo param in
someMethod.GetParameters()) Console.WriteLi
ne(param.ParameterType)
22
????????? ????????? ?? ????? ?? ??????? (Late
Binding)
  • ????????? ????????? ?? ????, ???? ????? ??
    ?????????? ?????? ?? ????? Activator
  • CreateInstance() ??????? ????????? ??
    ????????? ???
  • CreateInstanceFrom() ?????????? ????????? ???
    ?? ?????? ???????
  • CreateComInstanceFrom() ??????? ????????? ??
    COM ?????
  • ????????? ???????? ???????? ?? ???? ????
    System.MethodInfo.Invoke()

23
????????? ????????? ?? ?????
// Load the assembly mscorlib.dll Assembly
mscorlibAssembly Assembly.Load("mscorlib.dll
") // Create an instance of DateTime by
calling // new DateTime(2004, 1, 5) Type
systemDateTimeType mscorlibAssembly.
GetType("System.DateTime") object
constructorParams new object 2004, 1,
5 object dateTimeInstance
Activator.CreateInstance( systemDateTimeType,
constructorParams) (???????? ??????????)
????????? ?? ???????????? ?? DateTime
24
????????? ????????? ?? ?????
????????? ?? ??????, ????? ?????????. ???? ?? ???
??????? ?????? ? ??????? ???, ?? ? ????????
?????????.
// Invoke DateTime.AddDays(10) Type
addDaysParamsTypes new Type
typeof(System.Double) MethodInfo addDaysMethod
systemDateTimeType. GetMethod("AddDays",
addDaysParamsTypes) object addDaysParams new
object10 object newDateTimeInst
addDaysMethod.Invoke( dateTimeInstance,
addDaysParams) // Get the value of the property
DateTime.Date and print it PropertyInfo
datePropertyInfo systemDateTimeType.GetProp
erty("Date") object datePropValue
datePropertyInfo.GetValue( newDateTimeInst,
null) Console.WriteLine("0dd.MM.yyyy",
datePropValue)
GetValue() ???????? ?????? ?? ???????? ???? ???
?????????? ? ??????????
25
???????????? 4
  • ????????? ?? ??? ?? ??????? ? ????????? ?? ??????

26
????? ? Reflection Emit?
  • Reflection.Emit
  • ????????? ?? ???? ?????????
  • ????????? ?? ????????? ?? ?????
  • ?????????? ?? ?????????
  • ?????????? ? ????????? ?? ?????????
  • Reflection.Emit ?? ????????? ?? ????????
    ????????? ?? ??????
  • ??????
  • ??????
  • ????????????
  • ??????
  • ???????
  • ????????

27
?????????? ?? Reflection Emit
  • ?????????????? System.Reflection. Emit ??????????
    ????? ?? ??????? ?? ????????? ?? ????? ??
    ?????????
  • ????????? AssemblyBuilder
  • ?????? ModuleBuilder
  • ?????? TypeBuilder
  • ???????????? ConstructorBuilder
  • ?????? MethodBuilder
  • ???????? PropertyBuilder
  • ??????? EventBuilder

28
?????????? ?? Reflection Emit
  • ???? ????? System.Reflection.Emit. ILGenerator ??
    ????????? MSIL ??????????
  • ????????????? MSIL ???????? ???
  • ????? ?? ?? ??????? ? ????? ?????
  • Emit() ???????? ? ????? ???????????????? ??
    MSIL ??????????
  • EmitWriteLine() ?????? ???????????? ??
    ??????????? ?? ???
  • ????????? ?? ????????? ?????????
  • AssemblyBuilder.SetEntryPoint()

29
????????? ?????????? ?? ???????
AssemblyName assemblyName new
AssemblyName() assemblyName.Name
"DynamicAssembly" AssemblyBuilder newAssembly
AppDomain. CurrentDomain.DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.RunAndSave
) ModuleBuilder newModule newAssembly.DefineD
ynamicModule( "NewModule", "
EmitedAssembly.exe ") TypeBuilder newType
newModule.DefineType( "HelloWorldType",
TypeAttributes.Public) MethodBuilder newMethod
newType.DefineMethod( "WriteHello",
MethodAttributes.Static MethodAttributes.Publi
c, null, null) (???????? ??????????)
30
????????? ?????????? ?? ???????
ILGenerator msilGen newMethod.GetILGenerator()
msilGen.EmitWriteLine( "Hello World! Today is "
DateTime.Now) msilGen.Emit(OpCodes.Ret) Type
helloWorldType newType.CreateType() Object
instance Activator.CreateInstance(helloWorldTy
pe) MethodInfo helloWorldMethod
helloWorldType.GetMethod("WriteHello") helloWorld
Method.Invoke(instance, null) newAssembly.SetEnt
ryPoint(helloWorldMethod) newAssembly.Save("Emite
dAssembly.exe")
31
???????????? 5
  • ????????? ????????? ?? ???????

32
????????? ?? ????????(Reflection)
????????
33
??????????
  1. ????? ? Global Assembly Cache? ?? ????? ??????
  2. ??????? ???? ???? ????? ?? ??????????? ??
    ??????????? ?? Global Assembly Cache.
  3. ?? ?? ????????? Windows Forms ??????????, ?????
    ????????? ?? ?? ??????? ??????? ?? ???????????
    ??????? ? ??????? ?????????? ?? ???? (??? ??
    ?????? ? ????????, ???? ? ???????? ?? GAC,
    ???????? ?? ????? ? ?.?.) .
  4. ?? ?? ????????? ???????? ??????????, ?????
    ??????? ????????? mscorlib.dll ? ?????????
    ??????? ?? ?????? ?????? ? ????.
  5. ?? ?? ????????? ???????? ??????????, ?????
    ??????? ????????? mscorlib.dll ? ?????? ??????
    ?????? ?? ???? System.DateTime, ????? ? ?????????
    ? ????.

34
??????????
  1. ????????? Windows Forms ??????????, ????? ???????
    ???????, ????? ?? ????? ?? ?????? ?? ???????????
    ? ??????? ?? ???? ??????? ? ??????????? ??
    ?????????????? ?? ?????? ??????, ?????????? ?
    ????.
  2. ??????????? ????????? ICalculatable, ?????
    ???????? ????? double Calculate(int). ????????
    ???????? ??????????, ????? ???? ?? ??????? ????
    ?????? ?? ?????, ?????? ?????? ????????? ??
    ???????? ??????????, ? ????? ??? ????????????? ??
    ICalculatable ? ???? ????? ?? ??? ????????
    ???????????? Calculate() ? ????????? ?????????.
    ????????? ???? ????????? ??? ?????????, ? ?????
    ??? ???, ????????????? ICalculatable. ??????
    ??????? ?????? ?? ????????? ?????? ???????????, ?
    ??????? ???? ?? ?????????? ?? ????????? ?????.

35
??????????
  1. ????????? ????????, ????? ??????? ????????
    ???????? ???????????????? ? ??????? ???????
    ????????? ???, ????? ??????? ????? ??????????
    ???? ???????? ????????????????. ????????????
    ??????? ?????? ?? ???? ?????????, ???? ????????
    ????.

36
?????????? ??????????
  • Jeffrey Richter, Applied Microsoft .NET Framework
    Programming, Microsoft Press, 2002, ISBN
    0735614229
  • Jesse Liberty, Programming C, 3rd Edition,
    O'Reilly, 2003, ISBN 0596004893
  • Professional C, 3rd, Wrox Press, 2004, ISBN
    0764557599
  • ?????? ??????, ????????? ?? ???????? (Reflection)
    http//www.nakov.com/dotnet/2003/
    lectures/Reflection.doc
  • MSDN Library http//msdn.microsoft.com
Write a Comment
User Comments (0)
About PowerShow.com