首页 > 解决方案 > FieldInfo.GetRawConstantValue 在 UWP 上不可用

问题描述

我需要在我的类中获取一个常量的值。 GetField工作正常。

但是myFieldInfo.GetRawConstantValue()通常可以完美运行,但在 UWP 上不可用。

有没有办法在 UWP 上实现这一点?

标签: c#reflectionuwpconstantsclass-constants

解决方案


有没有办法在 UWP 上实现这一点?

源自本文档GetRawConstantValue方法适用于.NET Standard。因此,您可以创建.NET Standard可以被 UWP 项目引用的类库。

public class LibCore
{
    public static object GetRawConstantValue(Type target, string filedName)
    {
        var filed = target.GetField(filedName);
        var value = filed.GetRawConstantValue();
        return value;
    }
}

用法

var value = LibCore.GetRawConstantValue(typeof(Person), "Name");

注意:如果.NET Standard类库版本为2.0,则需要修改uwp min version为16299。


推荐阅读