Question

I want to some how pass in the class type as a parameter into a method. Something like this

public void DoWork<TClass>(int time)
{
     IJobDetail job = JobBuilder.Create<TClass>()
                .WithIdentity("job1", "group1")
                .Build();
}

is it possible to get the classtype as a parameter or somehow get the class type by reflection of an object of that type?

UPDATE 1: I just tried to use a generic type and received this error

`The type TClass cannot be used as type parameter T in the generic type or method quartz.jobbuilder.create<T>().  There is now boxing conversion or type parameter conversion from tclass to quartz.ijob`

Create MEthod of Quartz

// Summary:
    //     Create a JobBuilder with which to define a Quartz.IJobDetail, and set the
    //     class name of the job to be executed.
    //
    // Returns:
    //     a new JobBuilder
    public static JobBuilder Create<T>() where T : IJob;
Was it helpful?

Solution

You're probably looking for a generic function -- this allows your function to specify a type parameter:

public void DoWork<TClass>(int time)
    where TClass : quartz.ijob
{
    IJobDetail job = JobBuilder.Create<TClass>()
        .WithIdentity("job1", "group1")
        .Build();
}

Edit

You can also place a restriction on the type parameter, which seems to be what is required here. Use the syntax where TClass : quartz.ijob.

Edit #2

Note that callers of a generic method must specify the type parameter. So, you'd have to call:

DoWork<SomeClassType>(123);

However, if the signature of the method includes a parameter of the generic type, then the compiler can use that to infer the type. So, if the signature is:

public void DoWork<TClass>(int time, TClass instance)
    where TClass : quartz.ijob

Then a caller could invoke it like this:

var myInstance = new SomeClassType();  // SomeClassType must inherit quartz.ijob
DoWork(123, myInstance);

Above, the type of myInstance is known at compile time, so the compiler can infer the generic type. However, if your type instance is boxed, then the type isn't known until runtime, and the compiler can't help you:

function SomethingOrOther(object instance)
{
    DoWork(123, instance);   // will not compile, as the compiler doesn't know the type of "instance"
}

In this scenario, it is possible to call DoWork anyway, by using reflection (see MakeGenericMethod). However, this approach of using reflection on generics gets complicated, fast. It also defeats the purpose of generics (ie, compile-time safety), and usually indicates a design flaw.

OTHER TIPS

Generally speaking, yes.

You can get type of class with typeof(YourClassHere) or type of object with yourObject.GetType().

Then you can easily create object with an acitvator: Activator.CreateInstance(type, parameters for constructor)

EDIT AFTER QUESTION EDIT It's even easier when you have the generic.

public void DoWork<TClass>(int time)
{
     IJobDetail job = (IJobDetail) Activator.CreateInstance(typeof(TClass), null /* no parameters */);
                job.WithIdentity("job1", "group1");
                job.Build();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top