如何避免同一作用域内重复输入相同名称?C#是否存在类似VBA With/End With的语法?
Great question! C# doesn’t have an exact equivalent of VBA’s With/End With structure, but there are several clean ways to eliminate that repetitive Event0Instance. prefix when initializing your dictionaries—plus bonus approaches for larger enums to avoid even more boilerplate code.
1. Use using static to import the enum type
The simplest fix for your immediate problem is to import the enum at the top of your file with the using static directive. This lets you reference enum values directly without repeating the type name:
// Replace "YourNamespace" with the actual namespace where Event0Instance is defined using static YourNamespace.Event0Instance; public static Dictionary<Event0Instance, string> Event0Descriptions = new Dictionary<Event0Instance, string>() { {ClaimSent, "Подана претензия" }, {RequestSent, "Подан запрос"}, {ClaimResponseAqcuired, "Получен ответ на претензию"}, {RequestResponseAqcuired, "Получен ответ на запрос"}, {RequestRefusalAcquired, "Получен отказ на запрос"} };
This keeps your dictionary initialization clean and cuts out the repetitive prefix entirely.
2. Use a helper method to auto-generate the dictionary (for large enums)
If you have lots of enums to handle, manually writing dictionary entries every time will get tedious. Instead, you can use attributes and reflection to auto-generate the description dictionary:
First, add DescriptionAttribute to your enum values:
using System.ComponentModel; public enum Event0Instance { [Description("Подана претензия")] ClaimSent, [Description("Подан запрос")] RequestSent, [Description("Получен ответ на претензию")] ClaimResponseAqcuired, [Description("Получен ответ на запрос")] RequestResponseAqcuired, [Description("Получен отказ на запрос")] RequestRefusalAcquired }
Then create a generic helper method to build the dictionary:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; public static class EnumHelper { public static Dictionary<TEnum, string> GetEnumDescriptions<TEnum>() where TEnum : Enum { return Enum.GetValues(typeof(TEnum)) .Cast<TEnum>() .ToDictionary( enumValue => enumValue, enumValue => { var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); var descriptionAttr = fieldInfo?.GetCustomAttribute<DescriptionAttribute>(); // Fall back to the enum name if no description is set return descriptionAttr?.Description ?? enumValue.ToString(); } ); } }
Now you can initialize your dictionary in one line—no manual entries needed:
public static Dictionary<Event0Instance, string> Event0Descriptions = EnumHelper.GetEnumDescriptions<Event0Instance>();
This is perfect for large enums, as it eliminates all repetitive boilerplate code.
3. C#'s closest equivalent to VBA's With
While C# doesn’t have With/End With, it does have object initializers which let you set multiple properties of an object without repeating the variable name. For example:
var person = new Person { FirstName = "John", LastName = "Smith", Age = 23 };
This achieves a similar goal of reducing repetition, but it’s limited to object initialization (you can’t use it for arbitrary code blocks like VBA’s With).
内容的提问来源于stack exchange,提问作者Igor Cheglakov

