将变化类型传递给泛型方法

时间:2014-07-01 08:24:05

标签: c# generics types

我确信这一定是可能的,但目前它已超出我的范围。

我有一种基于石英引擎初始化几个作业的方法。例如

private void InitialiseJobs(IScheduler scheduler)
{
    var ns = "Proto.QuartzScheduler.Domain.Jobs";
    var classname = "ExampleJob";

    var jobSetup = new JobSetup<type>(scheduler);
    jobSetup.Run(10);

    var second_jobSetup = new JobSetup<ExampleJob>(scheduler);
    second_jobSetup.Run(20);
}

这项工作的最后两行。我希望能够通过命名空间和类名定义类型,并传入泛型类型。例如,用我定义的任何内容替换ExampleJob

任何帮助表示赞赏。

道歉我应该给你所有的一点,而不是阻止下面的建议工作:

public class JobSetup<TType> where TType : IJobToDo
{

    private readonly IScheduler scheduler;
    private readonly IJobDetail jobDetail;
    private readonly JobKey jobKey;

    public JobSetup(IScheduler scheduler)
    {
        this.scheduler = scheduler;
        this.jobKey = new JobKey(Guid.NewGuid().ToString(), typeof(TType).ToString());
        this.jobDetail = JobBuilder
                            .Create<JobRunner<TType>>()
                            .WithIdentity(this.jobKey)
                            .Build();
    }

    public void Set<TPropertyType>(Expression<Func<TType, TPropertyType>> expression, TPropertyType value)
    {

        var memberExpression = expression.Body as MemberExpression;
        if (memberExpression == null)
        {
            throw new ArgumentException("Could not convert the expression to a member expression.");
        }

        this.jobDetail.JobDataMap[memberExpression.Member.Name] = value;
    }

    public TPropertyType Get<TPropertyType>(Expression<Func<TType, TPropertyType>> expression)
    {
        var memberExpression = expression.Body as MemberExpression;
        if (memberExpression == null)
        {
            throw new ArgumentException("Could not convert the expression to a member expression.");
        }

        return (TPropertyType)this.jobDetail.JobDataMap.Get(memberExpression.Member.Name);
    }

    public void Run(int number_of_seconds)
    {

        var trigger = new SimpleTriggerImpl
        (
            this.jobKey.Name,
            this.jobKey.Group,
            DateTime.UtcNow,
            null,
            SimpleTriggerImpl.RepeatIndefinitely,
            TimeSpan.FromSeconds(number_of_seconds)
        );

        this.scheduler.ScheduleJob(this.jobDetail, trigger);
    }
}

界面:

public interface IJobToDo
{
    void Run();
}

和ExampleJob

public class ExampleJob : IJobToDo
{

    public void Run()
    {
        Console.WriteLine("HelloJob is executing. {0}", System.DateTime.Now.ToUniversalTime());

    }
}

我没有为要激活的类设置无参数构造函数吗?

请保持好的建议

干杯

1 个答案:

答案 0 :(得分:0)

如果您的JobSetup<>泛型类实现了一些界面,如:

interface IJobSetup
{
    void Run(int x);
}

您可以通过界面使用Activator创建的对象,这样更优雅。所以写成:

private void InitialiseJobs(IScheduler scheduler)
{
    var classname = "Proto.QuartzScheduler.Domain.Jobs.ExampleJob";
    Type genericTypeParameter = Type.GetType(classname);
    Type genericClass = typeof(JobSetup<>);
    Type constructedClass = genericClass.MakeGenericType(genericTypeParameter);

    var setup = Activator.CreateInstance(constructedClass) as IJobSetup;
    setup.Run(20);
}