, , . Activator. Type.
:
static void Main()
{ // - System.Drawing.Rectangle
Assembly a = Assembly.LoadWithPartialName("System.Drawing");
AssemblyName an = a.GetName();
Type typeRect = Type.GetType("System.Drawing.Rectangle,"
+ an.FullName, true);
//
// Rectangle(Int32 x, Int32 y, Int32 width, Int32 height);
object[] ctorArgs = { 10, 10, 100, 100 };
Object rect = Activator.CreateInstance(typeRect, ctorArgs, null);
//
Console.WriteLine(Trace1.ObjectFields("rect", rect));
// Boolean Contains(Point pt);
System.Drawing.Point point = new System.Drawing.Point(50, 50);
object[] argContains = { point };
object contain = typeRect.InvokeMember("Contains"
, BindingFlags.Public
| BindingFlags.InvokeMethod
| BindingFlags.Instance
, null
, rect
, argContains
);
Console.WriteLine(contain.Equals(true)?
"contains": "does not contain");
Console.WriteLine(Trace1.ObjectFields("point", point));
Console.ReadLine();
}
, , , , .
. , , , .
:
static void Main()
{ // - System.Drawing.SolidBrush
Assembly a = Assembly.LoadWithPartialName("System.Drawing");
AssemblyName an = a.GetName();
Type typeBrush = Type.GetType("System.Drawing.SolidBrush,"
+ an.FullName, true);
//
// SolidBrush(Color color);
object[] ctorArgs2 = { System.Drawing.Color.Blue };
Object brush = Activator.CreateInstance(typeBrush
, ctorArgs2, null);
//
Console.WriteLine(Trace1.ObjectFields("brush", brush));
//
ICloneable cloner = brush as ICloneable;
if (cloner!= null)
{ Object brushCloned = cloner.Clone();
Console.WriteLine(Trace1.ObjectFields("brushCloned"
, brushCloned));
}
Console.ReadLine();
}
, , InvokeMember, . , , .
|
|
ICloneable as C#. , . , (null). .
. , , - . , FileProcessor. - . , -, , - - .
- , , . . -.
-:
// compile with csc /t:library FileProcessor.cs
using System;
namespace FileProc
{ interface IFileProcessor
{ bool IsSupport(string filePath);
}
}
. , MP. -, , .
:
// compile with csc /t:library TextProcessor.cs /r:FileProcessor.cs
using System;
using FileProc;
namespace TextProcessor
{ public class TextProcessor: IFileProcessor
{ override public bool IsSupport(string filePath)
{ return ".txt" == IO.Path.GetExtension(filePath).ToLower();
}
}
}
:
// compile with csc /t:library BmpProcessor.cs /r:FileProcessor.cs
using System;
using FileProc;
namespace BmpProcessor
{ public class BmpProcessor: IFileProcessor
{ override public bool IsSupport(string filePath)
{ return ".bmp" == IO.Path.GetExtension(filePath).ToLower();
}
}
}
, -, . , , -. , , IsSupport.
// compile with csc LateBind.cs /r:FileProcessor.cs
using System;
using System.IO;
using System.Reflection;
using FileProc;
namespace LateBind
{ class Class1
{ [STAThread] //
static void Main(string[] args)
{ if(args.Length < 1)
return;
string[] files = Directory.GetFiles(
System.Environment.CurrentDirectory, "*.dll");
foreach (string file in files)
{ Assembly assembly = Assembly.LoadFrom(file);
Type[] types = assembly.GetTypes();
foreach (Type type in types)
|
|
{ if (typeof(IFileProcessor).IsAssignableFrom(type)
&&! type.IsAbstract
)
{ IFileProcessor processor
= (IFileProcessor)Activator.CreateInstance(type);
if (processor.IsSupport(args[0]))
{ Console.WriteLine(
"Assembly {0} can process file {1}."
, assembly.GetName().Name, args[0]);
Console.ReadLine();
return;
}
}
}
}
Console.WriteLine("Can't process file {0}.", args[0]);
Console.ReadLine();
}
}
}
, , , VRML, . - VRMLProcessor , -.