[英]Selecting multiple months for a MonthlyTrigger using css
我需要使用C#應用程序創建計划的Windows任務。 我有一個用逗號分隔的字符串,用於存儲要運行任務的月份。 該字符串包含MonthsOfYear類型的短值-例如。 “1,2,4,16,128,1024”。
我的示例顯示,您可以分配多個由管道分隔的月份,如下所示:
MonthlyTrigger mt = new MonthlyTrigger();
mt.StartBoundary = Convert.ToDateTime(task.getStartDateTime());
mt.DaysOfMonth = new int[] { 10, 20 };
mt.MonthsOfYear = MonthsOfTheYear.July | MonthsOfTheYear.November;
我的問題是,如何使用逗號分隔的字符串中的值動態地將多個月分配給觸發器。
我不太確定,您的問題是什么。 而且您沒有發布觸發器或枚舉的代碼。 因此,我將提供一個完整的示例,並帶有用於比較的列表:
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;
}
在枚舉中添加或刪除單個標志:
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.