简体   繁体   English

使用CSS为MonthlyTrigger选择多个月

[英]Selecting multiple months for a MonthlyTrigger using css

I have the need to create scheduled windows tasks using a C# app. 我需要使用C#应用程序创建计划的Windows任务。 I have a comma separated string that stores the months I'd like to run the task on. 我有一个用逗号分隔的字符串,用于存储要运行任务的月份。 The string contains the short values for the type MonthsOfYear - eg. 该字符串包含MonthsOfYear类型的短值-例如。 "1,2,4,16,128,1024". “1,2,4,16,128,1024”。

The example I have shows that you can assign multiple months seperated by a pipe as follows: 我的示例显示,您可以分配多个由管道分隔的月份,如下所示:

MonthlyTrigger mt = new MonthlyTrigger();
mt.StartBoundary = Convert.ToDateTime(task.getStartDateTime());
mt.DaysOfMonth = new int[] { 10, 20 };
mt.MonthsOfYear = MonthsOfTheYear.July | MonthsOfTheYear.November;

My question is, how do I assign multiple months to the trigger dynamically, using the values from the comma seperated string. 我的问题是,如何使用逗号分隔的字符串中的值动态地将多个月分配给触发器。

I'm not quite sure, what your problem is. 我不太确定,您的问题是什么。 And you didn't post code of your Trigger or your enum. 而且您没有发布触发器或枚举的代码。 Because of this i'll provide a complete example with a List for comparesion: 因此,我将提供一个完整的示例,并带有用于比较的列表:

public class MonthlyTrigger
{
    [Flags] // Important because we want to set multiple values to this type
    public enum MonthOfYear
    {
        Jan = 1, // 1st bit
        Feb = 2, // 2nd bit..
        Mar = 4,
        Apr = 8,
        May = 16,
        Jun = 32,
        Jul = 64,
        Aug = 128,
        Sep = 256,
        Oct = 512,
        Nov = 1024,
        Dec = 2048
    }

    public HashSet<int> Months { get; set; } = new HashSet<int>(); // classical list to check months
    public MonthOfYear MonthFlag { get; set; } // out new type
}

public static void Main(string[] args)
{
    MonthlyTrigger mt = new MonthlyTrigger();

    string monthsFromFileOrSomething = "1,3,5,7,9,11"; // fake some string..

    IEnumerable<int> splittedMonths = monthsFromFileOrSomething.Split(',').Select(s => Convert.ToInt32(s)); // split to values and convert to integers

    foreach (int month in splittedMonths)
    {
        mt.Months.Add(month); // adding to list (hashset)

        // Here we "add" another month to our Month-Flag => "Flag = Flag | Month"
        MonthlyTrigger.MonthOfYear m = (MonthlyTrigger.MonthOfYear)Convert.ToInt32(Math.Pow(2, month - 1));
        mt.MonthFlag |= m;
    }

    Console.WriteLine(String.Join(", ", mt.Months)); // let's see our list
    Console.WriteLine(mt.MonthFlag); // what is contained in our flag?
    Console.WriteLine(Convert.ToString((int)mt.MonthFlag, 2)); // how is it binarily-stored?


    // Or if you like it in one row:
    mt.MonthFlag = 0;
    foreach (MonthlyTrigger.MonthOfYear m in monthsFromFileOrSomething.Split(',').Select(s => (MonthlyTrigger.MonthOfYear)Convert.ToInt32(s)))
        mt.MonthFlag = m;

    return;
}

Adding or removing single Flags in an enum: 在枚举中添加或删除单个标志:

MyEnumType myEnum = 1; // enum with first flag set

myEnum |= 2; // Adding the second bit. Ofcouse you can use the enum-name here "MyEnumType.MyValueForBitTwo"
// Becuase:
//   0000 0001
// | 0000 0010   "or"
// = 0000 0011

myEnum &= (int.MaxValue - 2) // Deletes the second enum-bit.
// Because:
//   0000 0011
// & 1111 1101   "and"
// = 0000 0001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM