Title: Web Services with ASP.NET
1(No Transcript)
2Reflection
???????????? ?? .NET Framework
http//www.nakov.com/dotnet/
?????? ???????
3?????????? ??????
- ?????? ???????? ?? .NET Framework
- ?????? ???????? ?? ????? C
- ?????? ???????? ?? MSIL
4??????????
- ????? ? Global Assembly Cache
- ????? ? Reflection
- ????????? ?? ?????????
- ????????? ?????????? ?? ???????
- ?????????? ?? ????????? ?? ???????
- ????????? ?? ???????? ? ???????
- ????????? ??????????? ?? ????? ?????
- ????????? ????????? ?? ?????
- ????? ? Reflection Emit
5????? ? Global Assembly Cache
- GAC ? ????????? ?? ?????????? ?? ?????????
????????????? ?? ?????????? ?? ?????????
?????????? - ?? ????????? ????????? ? GAC ?????, ??? ?? ?
????????? ??????????
6???????????? 1
- ??????? ?? GAG ???? Windows Explorer ?
Administrative Tools
7????? ? Reflection
- ??????????? Reflection ???? ?????????? ???? ??
- ??????? ??????????? ?? ?????????
- ??????? ???????? ? ?????? ???????
- ????????? ?? ??????? ??????
- ????????? ?? ??????? ???? ????????? ? ?? ??
???????? ??? ???????
8????????? ?? ?????????
- ????????? ???? System.Reflection.Asembly.Load
- ??? ????????? ??????? ? ????? ????????? ?? ?????
? - GAC (Global Assembly Cache)
- ???????? ??????????
- ?????? ?? ???????????? PATH
- ??? ????????? ??????? ? ????? ?????????, ?? ??
????????? GAC - ??? ????????? ?? ???? ???????? ???????????
System.IO.FileNotFoundException
Assembly.Load(MyAsmb.dll)
9????????? ?? ?????????
- ????????? ???? LoadFrom
- ?????? ???? ????????? ???? ?? ?????????
- ??????? ????????? ????
- ??????? ???????? Load
- ??-????? ?? Load
- ??? ????????? ?? ???? ???????? ?????
System.IO.FileNotFoundException
Assembly.LoadFrom(c\MyProject\MyAsmb.dll)
10????????? ?????????? ?? ???????
- ???????? ?? System.Reflection.Assembly ??
????????? ?????????? ?? ??????? - FullName
- - ??????? ??? ?? ?????????, ?????????
??????, ??????? ? ???? - 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?????????? ?? ????????? ?? ??????? (Unload)
- ?? ?? ??????? ???????????? ?? ?? ???????? ????
???????. - ???????? ? ???????????? ?? ?????? ????????? ?
????? AppDomain - ?? ?? ?????????? ?? ?? ???????? ?????? ????????
???????? ?? ??????.
16????????? ?? ???????? ? ???????
- System.Type ???????? ????? ?? ?????????? ??
??????????? ? ?????? ? ??????. - ???? System.Type - ????? ?? ??????? ??????
??????? ?? ????? ??? - ??????
- ????????
- ??????
- ???????
- ???? ?????? Assembly.GetTypes() ??????????
???????? ??????? ? ?????? ???????
17????????? ?? ???????? ? ???????
- Sytem.Type ???????? ????????? ?? ???????? ??
????????? ???????????? ?? ????? ??? - BaseType, DeclaringType, FullName, GUID,
IsAbstract, IsArray, IsAutoClass, IsByRef,
IsClass, IsCOMObject, IsEnum, IsImport ,
IsInterface, IsNestedAssembly, IsNestedPrivate,
IsNotPublic, IsPointer, IsPublic, IsSealed,
IsSpecialName, IsValueType........
18????????? ????????? ?? ???
?????????? ?????? ?????? ? ?????????
??????? ???????? ???????
Assembly MyAssembly Assembly.GetExecuting
Assembly() foreach(Type MyType in
MyAssembly.GetTypes()) foreach(MemberInfo mm
in MyType.GetMembers())
Console.WriteLine(mm.MemberType )
Console.WriteLine(mm.Name )
GetMembers() ????? ????? ?? ?????? ??????? ?? ????
19???????????? 3
- ????????? ???????? ?? ???????
20??????? ?? ???????? ???????
- ?? ?????????? ????????? ??????? ?? ????? ??? ???
?? ???? ????, ????? ?? ??????
21????????? ??????????? ?? ????? ?????
- ??????????? MethodInfo.GetParameters() ?????????
??????????? ?? ????? ?????
MethodInfo myMethod myType.GetMethod("MyMethod")
foreach(ParameterInfo param in
myMethod.GetParameters())
Console.WriteLine(param.ParameterType)
22????????? ????????? ?? ????? ?? ??????? (Late
Binding )
- ????????? ????????? ?? ????, ???? ????? ??
???????? ???????? ?????? ?? ????? Activator - CreateComInstanceFrom ???????? ?? ?? ?????????
?? ????????? ?? COM ????? - CreateInstanceFrom - ????????? ?? ????????? ??
?????????? ??????? ? ??? - CreateInstance ??????? ????????? ?? ?????
- ????? ????????? ?? ???????? ???????? ?? ?????
??? ???? System.MethodInfo.Invoke()
23????????? ????????? ?? ?????
Assembly MscorlibAssembly Assembly.Load("Ms
corlib.dll") Type SystemDateType
MscorlibAssembly.GetType("System.DateTime")
Object consParams new Object3
consParams0 2004 consParams1 1
consParams2 05 Object inst
Activator.CreateInstance(SystemDateType,
consParams) (???????? ??????????)
????????? ?? ???????????? ?? DateTime
24????????? ????????? ?? ?????
????????? ?? ??????, ????? ?????? ?? ??????????.
????? ? ?? ??? ??????????, ?????? ???? ?? ??? ???
?????? ? ??????? ???, ?? ? ???????? ?????????.
- Type paramTypes new Type1
- paramTypes0 Type.GetType("System.Double")
- MethodInfo AddDayInfo
SystemDateType.GetMethod("AddDays,paramTypes ) - Object parameters new Object1
- parameters0 19
- inst AddDayInfo.Invoke(inst, parameters)
- PropertyInfo DInfo SystemDateType.GetProperty("D
ate") - Console.WriteLine("Date is 0",
DInfo.GetValue(inst,null ))
??????? GetValue ???????? ?????? ?? ????????
???? ??? ?????????? ? ???????????
25???????????? 4
26????? ? Reflection Emit
- Reflection.Emit
- ????????? ?? ???? ?????????
- ????????? ?? ????????? ?? ?????
- ?????????? ?? ?????????
- ?????????? ? ????????? ?? ?????????
- Reflection.Emit ?? ????????? ?? ????????
????????? ?? ??????. - ??????
- ??????
- ????????????
- ??????
- ???????
- ????????
27?????????? ?? Reflection Emit
- ??????????? ?? ???, ????? ??????? ?????? ??????
???????
AssemblyName AsmbName new AssemblyName() AsmbNa
me.Name "MyDynamicAsmb"
- ????????? ??????????? ???????
AssemblyBuilder MyAsmb AppDomain.CurrentDoma
in.DefineDynamicAssembly ( AsmbName ,
AssemblyBuilderAccess.Run )
28?????????? ?? Reflection Emit
- ????????? ??? ????? ? ?????????
ModuleBuilder MBuilder AsmbBuilder.DefineDynam
icModule("MyModule")
AssemblyBuilder MyAsmb AppDomain.CurrentDomain.
DefineDynamicAssembly( AsmbName ,
AssemblyBuilderAccess.Run )
- ?? ?????????? ????? ?????????
- ??????????? - ConstructorBuilder
- ??????? - EventBuilder
- ???????? - PropertyBuilder.
- ????? - MethodBuilder
29?????????? ?? Reflection Emit
- ????????? ?? ????????? ???? Class Library
(dll ???????) ?? ???????????? - ????????? ?? ????????? ????????? ?????? ??
???????? ?????? ????? ?? ????????? ? - AssemblyBuilder.SetEntryPoint
30?????????? ?? MSIL ??????????
- ??????????? ????? System.Reflection.Emit.ILGenerat
or ?? ????????? MSIL ??????????, ????? ????? ??
??????? ? ?????? ??????? - Emit ???????? ? ????? ???????????????? ?? MSIL
?????????? - EmitWriteLine ?????? ???????????? ??
??????????? ?? ??????.
31???????????? 5
32Reflection
????????
33??????????
- ???????? ????????, ????? ??????? Mscorlib. ??????
???????? ?????? ?? ????????? ??????? ?? ????????
?????? ? ??????? ??? ????. - ???????? ?? ????? ?? ?????????? ?? ?????????? ??
Mscorlib, ? ???????? ???????? ?? ?????? ?????????
? Get, ????? ? ?????? ?????????? ?????? - ?? ?? ?????? ?????????, ????? ?????? ?????????
System.Windows.Forms.dll, ??????? ?? ???? ????
"Label" ? ????????? ???????? ?? ?????? ?????? ?
??????? ?? ????????? ?? C
34??????????
- 4. ???????? ??? ????????? (Class Library), ?????
????????? ??? SumType ??? ????? ?????????
?????? ??? ???? ????? ??? ??? ??????????.
???????? ????????, ????? ????????? ?? ???????????
?? ???? ????? ??? ????? ???? ?? ??????. ??????
?????? ?? ??????????? ???????? ???????????
??????? ? ??????????? ?????????? ??? ??
?????????.
35??????????
- 5. ???????? ???? "AverageCalculator", ?????
??????? ?????????? ?? ???? ????? ? ????????
???????? Add(int), Add(int) ?? ???????? ??
????? ? ???????? Average, ????? ??????? ????????
??????????? ?? ???? ?????. ???????????? ????? ?
??????? ? ??? "AverageCalculator.dll". ????????
????, ????? ????????? ??????? ?? ?????????
AverageCalculator.dll ????? AverageCalculator ? ?
?????? ????? ???????? ???????? ??????????? ??
???????? ????? 5,6,6,5,3,4,5,6,6
36??????????
- 6. ???????? ????????, ????? ??????? ????????
???????? ???????????????? ? ??????? ???????
????????? ???, ????? ??????? ????? ??????????
???? ???????? ????????????????. ???? ??????? ??
???? ?????????, ???? ????????? ??????? (?? ??
??????? executable ????).
37?????????? ??????????
- Jeffrey Richter, Applied Microsoft .NET Framework
Programming, Microsoft Press, 2002 - Programming C, 3rd Edition By Jesse Liberty,
Publisher O'Reilly Date Published May 2003 - Professional C, Third Edition, Wrox Press
- ????????? (Reflection) ?????? ??????,
http//www.nakov.com/dotnet/2003/lectures/Reflecti
on.doc