在C#中,是否可以返回对静态类的引用?
我希望下面的LinqPad示例(无法编译)解释我的想法:
void Main()
{
var x = GetConstants();
x.C1.Dump();
}
public Type GetConstants() =>
(DateTime.Now.DayOfWeek == DayOfWeek.Saturday
|| DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
? AllConstants.WeekendConstants
: AllConstants.WeekdayConstants;
}
public static class AllConstants
{
public static class WeekdayConstants
{
public const string C1 = "Weekday_C1";
public const string C2 = "Weekday_C2";
}
public static class WeekendConstants
{
public const string C1 = "Weekend_C1";
public const string C2 = "Weekend_C2";
}
}
答案 0 :(得分:0)
要使用类型Type
(在这种情况下,这似乎是正确的选择),您必须将类名包装在typeof
中。这样做将导致typeof(AllConstants.WeekendConstants)
并在编译时返回所述类的类型 。
请记住,该不会使您可以像在Main
方法中尝试的那样直接访问常量。要获得这些,您必须查看this question。
但是,我对typeof
的使用将为您提供所需的Type
对象,然后获取该类的所有常量。
编译代码如下:
如前所述,您将无法直接调用常量,因为这将仅返回代表静态类的Type
。
public Type GetConstants() =>
(DateTime.Now.DayOfWeek == DayOfWeek.Saturday
|| DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
? typeof(AllConstants.WeekendConstants)
: typeof(AllConstants.WeekdayConstants);
也在旁注中。正如BACON的评论中已经提到的那样,为此使用带有constans的静态类可能不是最佳解决方案。从外观上看,我认为最适合字典。想像这样:
public static class WeekConstants
{
public static Dictionary<string, string> Weekdays { get; } = new Dictionary<string, string>()
{
{ "C1", "Weekday_C1" },
{ "C2", "Weekday_C2" },
};
public static Dictionary<string, string> Weekends { get; } = new Dictionary<string, string>()
{
{ "C1", "Weekend_C1" },
{ "C2", "Weekend_C2" },
};
}
在此代码中,我使用一个只读属性(更多here)。显然这不是常数,而是使用字典可获得的最接近的常数。
使用Enum
代替字符串作为键也可能是一个好主意,因此您可以使用DayEnum.C1
而不是"C1"
来要求一个工作日。
答案 1 :(得分:0)
如果目标是确保使用的值存储在常量中,则可以实现这一点而不必遍历包含这些常量的类型。如果您的方法返回Type
,则必须使用反射来尝试在这些类型中查找所需的常量。您不能让它们表现得像接口或非静态类实例。
您可以定义一个表示这些值的集合的类型,然后使用您的常量填充它:
public class ThingWithValues // because I don't know what these things are
{
public ThingWithValues(string c1, string c2)
{
C1 = c1;
C2 = c2;
}
public string C1 { get; }
public string C2 { get; }
}
public ThingWithValues GetValues() =>
(DateTime.Now.DayOfWeek == DayOfWeek.Saturday
|| DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
? new ThingWithValues(AllConstants.WeekendConstants.C1, AllConstants.WeekendConstants.C2)
: new ThingWithValues(AllConstants.WeekdayConstants.C1, AllConstants.WeekdayConstants.C2);
由于它们只有两种变化-周末和工作日,您甚至可以将它们定义为静态成员,如下所示:
public class ThingWithValues
{
public ThingWithValues(string c1, string c2)
{
C1 = c1;
C2 = c2;
}
public string C1 { get; }
public string C2 { get; }
public static ThingWithValues Weekend { get; } =
new ThingWithValues(AllConstants.WeekendConstants.C1, AllConstants.WeekendConstants.C2);
public static ThingWithValues Weekday { get; } =
new ThingWithValues(AllConstants.WeekdayConstants.C1, AllConstants.WeekdayConstants.C2);
}
然后用法变为
public ThingWithValues GetValues() =>
(DateTime.Now.DayOfWeek == DayOfWeek.Saturday
|| DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
? ThingWithValues.Weekend
: ThingWithValues.Weekday;
或将“是周末”支票添加到ThingWithValues
中的另一个静态成员中:
public static ThingWithValues ForDate(DateTime date) =>
date.DayOfWeek == DayOfWeek.Saturday
|| date.DayOfWeek == DayOfWeek.Sunday
? Weekend
: Weekday;
现在用法变为
var x = ThingWithValues.ForDate(DateTime.Now);