public, public readonly, public static, public const

博客给出了ClassA类的代码,包含多种类型的变量。介绍了不同类型变量的使用方式,对于public static或public const变量,需通过类名引用;对于public但非static和const的变量,需通过类的新实例引用,还说明了读写情况。

public class ClassA

{

public string variableA;

public readonly string variableB = "variabl B";

public string variableB1{ get { return "variabe B1"; } }

public static string variableC;

public const string variableD;

}

how should use these variables?

for variables with public static or public const, must reference the class, e.g: ClassA.variableC, ClassA.variableD.

for variables with public but without staitc and const, must reference with a new instance, e.g:

ClassA myClassA = new ClassA(); // create an instance

myClassA.variableA = "variable A"; string variableA = myClassA.variableA; // both get and set( i.e. read and write)

string variableB = myClassA.variableB; string variableB1 = myClassA.variableB1; // only get (i.e. read only)

<think>首先,用户的问题是:“我想查找使用C#获取public const成员的FieldInfo的方法 请问C#获取public const成员的FieldInfo的方法”。这是在询问如何在C#中获取public const成员的FieldInfo。 我需要回顾系统级指令: - 所有行内数学表达式必须使用$...$格式。 - 独立公式必须使用$$...$$格式并单独成段。 - LaTeX语法正确。 - 使用中文回答。 - 生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识。 用户提供了几个引用: - 引用[^1]:展示了使用 `typeof(SpiderErrorNoConstant).GetFields()` 来获取字段信息,并迭代输出const常量的名称和值。 - 引用[^2]:讨论了Name属性和反射方法。 - 引用[^3]:关于Java方法签名错误,但用户提到参考站内引用,所以可能与反射相关。 - 引用[^4]:C#图解教程中的示例,涉及隐式运算符和常量。 用户上一次的问题和我的回答:用户上次的问题是查找C#获取public const成员的FieldInfo的方法,但我没有之前的回答历史,所以我可以直接从当前信息构建回答。 关键点:用户想知道如何获取public const成员的FieldInfo。从引用[^1]中,有一个代码示例: ```csharp Type t = typeof(SpiderErrorNoConstant); FieldInfo[] fis = t.GetFields(); foreach (var fieldInfo in fis) { Console.WriteLine(fieldInfo.Name + "=" + fieldInfo.GetRawConstantValue().ToString()); } ``` 这表明使用 `GetFields()` 方法获取所有字段,然后过滤或直接访问const成员。注意,引用提到“这里不能有任何选项,否则将无法获取到const常量”,意思是默认行为获取所有公共字段,包括const。 在C#中,常量(const)是隐式静态的,并且是字段。所以,`GetFields()` 方法会返回它们。要获取public const成员,我可以直接在类型上调用 `GetFields()`。 回答结构: 1. **清晰的结构**:逐步解释如何实现。 2. **使用中文**:整个回答用中文。 3. **引用标识**:在回答中引用内容时添加引用标识。 4. **生成相关问题**:在结尾添加相关问题。 步骤: - 解释如何获取类型。 - 使用 `GetFields()` 获取所有公共字段。 - 筛选或直接访问const字段(尽管const字段会被包括在内)。 - 使用 `GetRawConstantValue()` 获取值。 - 提供代码示例。 从引用[^1],使用 `GetRawConstantValue()` 而不是 `GetValue(null)`,因为 const 是静态的。`GetValue` 需要实例,但 const 是静态的,所以用 `GetRawConstantValue()`。 在C#中,要获取 const 字段的值,最好使用 `GetRawConstantValue()` 因为它专门用于编译时常量。 回答内容: - 介绍反射的概念。 - 步骤: 1. 获取类型:`Type t = typeof(YourClass);` 2. 获取所有公共字段:`FieldInfo[] fields = t.GetFields();` (默认获取公共实例和静态字段)。 3. 迭代所有字段,检查是否是 const(可选,但可以直接使用)。 4. 获取值:`fieldInfo.GetRawConstantValue()` - 提供完整示例。 - 注意:引用[^1]强调不能有选项,意思是使用默认的 `GetFields()`,没有 BindingFlags,否则可能不包括 const。 在C#中,如果指定 BindingFlags,可能会错过 const 字段。所以,默认行为正确。 生成相关问题: - 基于主题。 - 例如:如何筛选特定字段?如何使用反射修改 const(不可能)?等。 相关问题列表: 1. 如何判断一个字段是否为 const? 2. C#中 conststatic readonly 的区别? 3. 在反射中如何处理其他修饰符? 确保回答真实可靠:基于标准C#知识和引用。 最终回答结构: - 开头:简介。 - 步骤解释。 - 代码示例(代码块)。 - 注意事项。 - 相关问题。 </think>### C# 获取 public const 成员的 FieldInfo 方法 在 C# 中,获取 `public const` 成员的 `FieldInfo` 主要通过反射(Reflection)实现。`const` 成员是静态常量字段,您可以使用 `Type.GetFields()` 方法获取类型的所有公共字段(包括实例字段和静态字段),然后筛选或直接访问 `const` 字段。以下是详细步骤和说明: #### 步骤 1: 获取类型信息 首先,使用 `typeof()` 运算符获取目标类型(如类或结构体)。例如: ```csharp Type type = typeof(YourClassName); // 替换 YourClassName 为实际类型名 ``` #### 步骤 2: 获取所有公共字段 调用 `GetFields()` 方法获取该类型的所有公共字段(`public` 字段)。**注意**:必须使用默认方法(不带任何 `BindingFlags` 参数),否则可能无法获取到 `const` 常量[^1]。 ```csharp FieldInfo[] allFields = type.GetFields(); // 获取所有公共字段,包括 staticconst ``` #### 步骤 3: 筛选和访问 const 字段 迭代 `FieldInfo` 数组,每个字段的 `IsLiteral` 属性标识是否为常量(`const`)。使用 `GetRawConstantValue()` 方法获取常量的值(因为 `const` 是静态的,不能用 `GetValue()` 需要实例的方法)。 ```csharp foreach (FieldInfo field in allFields) { if (field.IsLiteral && field.IsPublic) // 检查是否为 public const { string name = field.Name; // 字段名 object value = field.GetRawConstantValue(); // 常量的值 Console.WriteLine($"{name} = {value}"); } } ``` #### 完整代码示例 以下是一个可运行示例,演示如何获取 `public const` 成员的 `FieldInfo`: ```csharp using System; using System.Reflection; public class SampleClass { public const int MaxValue = 100; // public const 成员 public const string DefaultName = "Test"; public int InstanceField = 10; // 实例字段(非 const) } class Program { static void Main() { Type type = typeof(SampleClass); FieldInfo[] fields = type.GetFields(); // 获取所有公共字段 Console.WriteLine("Public const 成员:"); foreach (var field in fields) { if (field.IsLiteral && field.IsPublic) // 确认是 public const { // 输出 FieldInfo 信息 Console.WriteLine($"名称: {field.Name}, 类型: {field.FieldType}, 值: {field.GetRawConstantValue()}"); } } } } ``` 输出结果: ``` Public const 成员: 名称: MaxValue, 类型: System.Int32, 值: 100 名称: DefaultName, 类型: System.String, 值: Test ``` #### 注意事项 - **`GetFields()` 不带参数**:如果添加 `BindingFlags`(如 `BindingFlags.Static | BindingFlags.Public`),可能导致 `const` 字段被忽略。默认行为已涵盖所有公共字段[^1]。 - **`GetRawConstantValue()` vs `GetValue()`**:常量是编译时常量,必须用 `GetRawConstantValue()` 获取值;`GetValue(null)` 可用于静态字段,但对 `const` 可能抛出异常。 - **性能考虑**:反射操作较慢,避免在性能敏感的代码中频繁使用。常量通常在编译时确定,反射主要用于调试或动态分析。 - **其他修饰符**:对于非 `const` 静态字段(如 `static readonly`),需用 `GetValue(null)` 获取值[^4]。 相关问题涉及反射和字段操作的深入应用场景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值