获取类成员的默认值

发布于 2024-09-04 04:32:06 字数 219 浏览 4 评论 0原文

假设我有一个 ClassWithMember 类,

class ClassWithMember
{
    int myIntMember = 10;
}

如何通过 System.Type 获取 myIntMember 成员的默认值 10?

我目前正在努力解决所有我检索到的反射,即 int (0) 的默认值而不是类默认成员 (10) ..

Let's assume I have a class ClassWithMember

class ClassWithMember
{
    int myIntMember = 10;
}

How do I get the default value 10 of the myIntMember member by System.Type?

I'm currently struggling around with reflections by all I retreive is the default value of int (0) not the classes default member (10)..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烦人精 2024-09-11 04:32:06

您可以尝试这样的事情:

var field = typeof(ClassWithMember).GetField("myIntMember",
    BindingFlags.Instance | BindingFlags.NonPublic);
var value = (int)field.GetValue(new ClassWithMember());

这里的技巧是实例化一个实例。

You can try something like this:

var field = typeof(ClassWithMember).GetField("myIntMember",
    BindingFlags.Instance | BindingFlags.NonPublic);
var value = (int)field.GetValue(new ClassWithMember());

The trick here is to instantiate an instance.

背叛残局 2024-09-11 04:32:06

尝试创建一个实例并通过反射检索值。

Try creating an instance an retreive the value with reflection.

盗琴音 2024-09-11 04:32:06

如果您控制 ClassWithMember 的代码,则可以使用 System.ComponentModel 中的 [DefaultValue] 属性,采取完全不同的方法。基本上,您要做的就是编写如下内容:

class ClassWithMember
{
    public ClassWithMember()
    {
        SetDefaultValues();
    }

    [DefaultValue(5)]
    public MyIntMember { get; set; }
}

然后在某处(也许在基类中)有一个类似的函数:

public void SetDefaultValues()
{
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this))
    {
        DefaultValueAttribute a = prop.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
        if (a == null) 
            continue;
        prop.SetValue(this, a.Value);
    }
}

所以现在,您可以使用反射轻松检索默认值。

请记住,由于反射要求,这会慢很多,因此如果此代码被实例化很多,您可能需要找到一种不同的方法。此外,由于 .NET Framework 属性支持的限制,它不适用于非值类型。

If you're in control of the code for ClassWithMember, you could take a completely different approach to this by using the [DefaultValue] attribute from System.ComponentModel. Basically, what you'd do is write something like this:

class ClassWithMember
{
    public ClassWithMember()
    {
        SetDefaultValues();
    }

    [DefaultValue(5)]
    public MyIntMember { get; set; }
}

And then have a function like this somewhere, perhaps in a base class:

public void SetDefaultValues()
{
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this))
    {
        DefaultValueAttribute a = prop.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
        if (a == null) 
            continue;
        prop.SetValue(this, a.Value);
    }
}

So now, you have a situation where you can easily retrieve the default values using Reflection.

Keep in mind that this is going to be quite a lot slower due to the Reflection requirement, so if this code gets instantiated a lot, you'll probably want to find a different approach. Also, it won't work with non-value types, due to a limitation with the .NET Framework's attribute support.

守望孤独 2024-09-11 04:32:06

如果只是为了检查默认值,您仍然可以使用 Activator.CreateInstance 创建 MonoBehaviour/ScriptableObject 并检查其值。确保事后使用 DestroyImmediate ;-)

You can still use Activator.CreateInstance to create a MonoBehaviour/ScriptableObject and check its values, if it's simply for the sake of checking the default Values. Make sure to use DestroyImmediate afterwards ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文