public class DivBy {
public bool IsDivBy(int a, int b) { if ((a % b) == 0) return true; return false;
}
public bool IsEven(int a) { if ((a % 2) == 0) return true; return false;
}
}
DLL MyClass.dll. , .
, MyClass. dll, .
// dynamic .
Using System;
Using System.Reflection;
class DynRefDemo { static void Main() {
Assembly asm = Assembly.LoadFrom("MyClass.dll");
Type[] all = asm.GetTypes();
// DivBy. int i;
for(i =0; i < all.Length; i++) if(all[i].Name == "DivBy") break;
if(i == all.Length) {
Console.WriteLine(" DivBy ."); return;
}
Type t = all[i];
// .
Constructorlnfo[] ci = t.GetConstructors();
Int j;
for(j =0; j < ci.Length; j++)
if(ci[j].GetParameters().Length == 0) break;
if(j == ci.Length) {
Console.WriteLine(" ."); return;
}
I
// DivBy , dynamic obj = ci[j].Invoke(null);
// obj. ,
// obj dynamic, // , , if(obj.IsDivBy(15, 3))
Console.WriteLine("15 3."); else
Console.WriteLine("15 HE 3.");
If(obj.IsEven(9))
Console.WriteLine("9 ."); else
, MyClass. dll, DivBy. obj dynamic. , Is DivBy () IsEven () obj , Invoke (). , obj DivBy. .
. , dynamic , . , , , , , dynamic, . , .
dynamic, , dynamic , . dynamic object , dynamic object . .
|
|
static void f(object v) { //... }
static void f(dynamic v) {//...}// !
: dynamic DLR (Dynamic Language Runtime ), .NET 4.0.
C# 4.0 , , () , , COM- Office Automation. , dynamic, , . COM- Office Automation , , . .
, , . , COM-, .
, C# get set. . , C# 4.0, COM- COM-, . , , . , myXLApp, Microsoft.Office. Inter op.Execl. Application.
"" 1- Excel , .
myXLapp.get_Range("Cl", "").set_Value(Type.Missing, "OK");
get Range (), . set_Value (), ( ) . Range Value, . , . , Type.Missing , , . , C# 4.0, , .
myXLapp.Range["Cl", ""].Value = "OK";
, Type.Missing , .
|
|
, ref . , , ref , . , , , ref .
. . , . , Internals Vi sibleTo.
I , C# .
lock
lock . 23, . .
C# . , , .. . , , . , . lock. :
lock(obj) {
//
}
obj , . , , . , . .
lock 23.
readonly
, readonly. , . , . , readonly . , , , . , readonly.
, readonly const, .
readonly. // readonly.
Using System;
class MyClass {
public static readonly int SIZE = 10;
class DemoReadOnly {
■ static void Main() {
int[] source = new int[MyClass.SIZE]; int[] target = new int[MyClass.SIZE];
// source, for(int i=0; i < MyClass.SIZE; i++) source[i] = i;
Foreach(int i in source)
Console.Write(i + " ");
Console.WriteLine();
// source target. for(int i = MyClass.SIZE-1, j = 0; i > 0; i, j++) target[j] = source[i];
|
|
Foreach(int i in target)
Console.Write(i + " ");
Console.WriteLine();
// MyClass.SIZE = 100; // !!! He !
}
}
MyClass.SIZE 10. , . , . .
const volatile
, , const , . . , const, , . , :
const int i = 10;
i const 10. const readonly, . readonly , const .
, , volatile , . , . , C# , , . , volatile.