i have the class:
public enum ProviderType { SqlClient, OracleClient};
public class ExternalClass
{
private string field1;
private string field2;
private string EnumsDependance;
public ExternalClass(string p1, string p2, ProviderType type)
{
this.field1 = p1;
this.field2 = p2;
if (type == ProviderType.SqlClient)
{
this.EnumsDependance = "SQL TYPE";
}
else if (type == ProviderType.OracleClient)
{
this.EnumsDependance = "ORACLE TYPE";
}
else
{
this.EnumsDependance = "NO TYPE";
}
}
}
In my program I need to create an instance of this class using reflection, but must pass the constructor of this class the value of the enum, how should I do this?
The program is in a different project ExternalClass and ProviderType, and it must be read by reflection. Here is my sketch:
class Program
{
static void Main(string[] args)
{
//dynamically load assembly from file ExternalDLL.dll
Assembly assembly = Assembly.LoadFile(@"C:\temp\ExternalDLL.dll");
//get type of class ProviderType (enum) from just loaded assembly
Type providerType = assembly.GetType("ExternalDLL.ProviderType");
//get type of class ExternalClass from just loaded assembly
Type externalClassType = assembly.GetType("ExternalDLL.ExternalClass");
//creating an instance of the class ExternalClass, using reflection
//constructor -> ExternalClass(string p1, string p2, ProviderType type)
object externalClassInstance = Activator.CreateInstance(externalClassType, new object[]{"param1",
"param2",
"ENUM VALUE" //how should I pass the value?
});
}
}
Aucun commentaire:
Enregistrer un commentaire