,
, , . where,
(, ),
. ,
- .
:
//
//
public static void Swap<T>(ref T a, ref T b)
where T: struct
{
Console.WriteLine("You sent the Swap()
method a {0}", typeof(T));
T temp;
temp = a;
a = b;
b = temp;
}
.
public struct Point<T>
{
//
private T xPos;
private T yPos;
//
public Point(T xVal, T yVal)
{
xPos = xVal;
yPos = yVal;
}
//
public T X
{
get { return xPos; }
set { xPos = value; }
}
public T Y
{
get { return yPos; }
set { yPos = value; }
}
public override string ToString()
{
return string.Format("[{0}, {1}]", xPos, yPos);
}
}
, .
public interface IBinaryOperations<T>
{
T Add(T arg1, T arg2);
T Subtract(T arg1, T arg2);
T Multiply(T arg1, T arg2);
T Divide(T arg1, T arg2);
}
public class BasicMath: IBinaryOperations<int>
{
public BasicMath() {}
// IBinaryOperations<int> Members
public int Add(int arg1, int arg2)
{ return arg1 + arg2; }
public int Subtract(int arg1, int arg2)
{ return arg1 - arg2; }
public int Multiply(int arg1, int arg2)
{ return arg1 * arg2; }
public int Divide(int arg1, int arg2)
{ return arg1 / arg2; }
}
18
- .
System.Reflection System.Type , , . , .
System.Type , . System.Reflection. : IsAbstract, IsArray, IsClass, IsEnum, IsValueType ( : , , ..), GetConstructors(), GetEvents(), GetFields(), GetInterfaces(), GetMethods(), GetProperties(), GetNestedTypes() ( , (, , ..). (, GetFields() FieldInfo, GetMethods() MethodInfo). , GetMethod().),GetType() ( Type ), InvokeMember() ( )
|
|
- , , .
- , , . , . , . .
. , .
public interface IAppFunctionality
{
void DoIt();
}
:
public class TheCSharpModule: IAppFunctionality
{
void IAppFunctionality.DoIt()
{
MessageBox.Show("You have just used the C# snap in!");
}
}
Windows Forms.
partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
private bool LoadExternalModule(string path)
{
bool foundSnapIn = false;
IAppFunctionality itfAppFx;
// .
Assembly theSnapInAsm = Assembly.LoadFrom(path);
//
Type[] theTypes = theSnapInAsm.GetTypes();
// , IAppFunctionality.
for (int i = 0; i < theTypes.Length; i++)
{
Type t = theTypes[i].GetInterface("IAppFunctionality");
if (t!= null)
{
foundSnapIn = true;
//
object o = theSnapInAsm.CreateInstance(theTypes[i].FullName);
// DoIt()
itfAppFx = o as IAppFunctionality;
itfAppFx.DoIt();
lstLoadedSnapIns.Items.Add(theTypes[i].FullName);
}
}
return foundSnapIn;
}
//
private void snapInModuleToolStripMenuItem_Click_1(object sender, EventArgs e)
{
//
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
if (LoadExternalModule(dlg.FileName) == false)
MessageBox.Show("Nothing implements IAppFunctionality!");
}
}
}