C # .NET runtime object type

I need to instantiate an object, and the type of this object will be determined at runtime. The type of the object is pulled from SQL and set to a string value. I also need to pass a number of parameters when creating it. The number / type of parameters will be the same every time (at least for now). What do I need for this, Activator.CreateInstance? Any help would be appreciated.

    private void StartScans(int scan_typeid, SqlDataReader drActiveServers)
    {
        string sql = "SELECT scan_typeclass from scan_types WHERE scan_typeid = " + scan_typeid.ToString();
        sqlconn.Open();
        SqlCommand cmd = new SqlCommand(sql, sqlconn);
        SqlDataReader drScanClass = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        string scan_class = drScanClass["scan_typeclass"].ToString();

        //Create object here

    }

EDIT:

Richard Berg's solution worked in a console application, but not in the above example, I reset scan_class and checked its value, however, I continue to receive this error:

System.ArgumentNullException: value cannot be null. Parameter Name: type

Here my updated code is as follows:

        try
        {
            string sql = "SELECT scan_typeclass from scan_types WHERE scan_typeid = " + scan_typeid.ToString();
            sqlconn3.Open();
            SqlCommand cmd = new SqlCommand(sql, sqlconn3);
            SqlDataReader drScanClass = cmd.ExecuteReader();
            drScanClass.Read();

            string scan_class = drScanClass["scan_typeclass"].ToString();

            var type = Type.GetType(scan_class);
            var myObj = Activator.CreateInstance(type, scan_id, scan_name, interval, drActiveServers);

        }
        catch (Exception e)
        {
            string sSource = "SharedAuditSVC";
            string sLog = "Application";
            string sEvent = e.ToString();

            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);

            EventLog.WriteEntry(sSource, sEvent);
            EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 0);

        }

, , , , . ..:)

:

WindowsServiceAudit WSA = new WindowsServiceAudit(scan_id, scan_name, interval, drActiveServers);

:

string scan_class = "WindowsServiceAudit";               

var type = Type.GetType(scan_class);
var myObj = Activator.CreateInstance(type, scan_id, scan_name, interval, drActiveServers);
+3
3

, .

var type = Type.GetType(scan_class);
var myObject = Activator.CreateInstance(type, constructorArg1, constructorArg2, [...] );

// use myObject - you'll have to reflect on any properties that aren't derived from System.Object

, , , , MethodInfo.Invoke() Activator.CreateInstance().

var scan_class = "WindowsServiceAudit";
var bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
var constructorArgs = new object[] { scan_id, scan_name, interval, drActiveServers };
var constructorTypes = from p in constructorArgs select p.GetType();

var type = Type.GetType(scan_class);            
var method = type.GetMethod(scan_class, bindingFlags, System.Type.DefaultBinder, constructorTypes.ToArray(), null);
var myObject = method.Invoke(null, bindingFlags, System.Type.DefaultBinder, constructorArgs, CultureInfo.CurrentCulture);

, :

  • . "" .
  • , . Assembly.Load(), .
+7

. , .

System.NullReferenceException: ​​ .   SharedAuditSVC.Scan.StartScans(Int32 scan_id, String scan_name, Int32 scan_typeid, Int32-, SqlDataReader drActiveServers) C:\shared_audit\SVC\SharedAuditSVC\SharedAuditSVC\Scan.cs: 84

84:

var method = type.GetMethod(scan_class, bindingFlags, System.Type.DefaultBinder, constructorTypes.ToArray(), null);

, , - , "WindowsServiceAudit". Windows, 2 .cs, Scan.cs WindowsServiceAudit.cs. Scan.cs - , , WindowsServiceAudit - , .

namespace SharedAuditSVC
{
    class Scan
    {

namespace SharedAuditSVC
{
    public class WindowsServiceAudit
    {

, . SharedAuditSVC.WindowsServiceAudit

, , , :

WindowsServiceAudit WSA = new WindowsServiceAudit(scan_id, scan_name, interval, drActiveServers);
0

Finally, this is correct :)

string sql = "SELECT scan_typeclass from scan_types WHERE scan_typeid = " + scan_typeid.ToString();
sqlconn3.Open();
SqlCommand cmd = new SqlCommand(sql, sqlconn3);
SqlDataReader drScanClass = cmd.ExecuteReader(CommandBehavior.CloseConnection);
drScanClass.Read();

var scan_class = "SharedAuditSVC." + drScanClass["scan_typeclass"].ToString();

Type[] argTypes = new Type[] { typeof(System.Int32), typeof(System.String), typeof(System.Int32), typeof(System.Data.SqlClient.SqlDataReader) };
object[] argVals = new object[] { scan_id, scan_name, scan_typeid, drActiveServers };
var type = Type.GetType(scan_class);
ConstructorInfo cInfo = type.GetConstructor(argTypes);
var myObject = cInfo.Invoke(argVals);

Now I can even make the arguments dynamic, after all. Thanks for the help!

0
source

Source: https://habr.com/ru/post/1714182/


All Articles