从反射fieldInfo获取实际类型

发布于 2024-12-06 08:10:11 字数 1506 浏览 0 评论 0原文

我正在围绕软件的 GUI API 编写一个通用包装器,它能够处理相当多的自己的“内置”类型,但我不知道如何获得它所需要的东西。例如,我这样做是为了处理字符串:

(“MakeTextField”和“MakeField”是伪代码,它们接受一个值,显示一个 GUI 元素,然后从 GUI 返回该值进行存储)

public static void FieldInfoMakeField<T>(T instance, System.Reflection.FieldInfo fieldInfo)
{
    string label = fieldInfo.Name;

    if (fieldInfo.FieldType == typeof(string))
    {
        var val = (string)fieldInfo.GetValue(instance);
        val = MakeTextField(label, val);
        fieldInfo.SetValue(instance, val);
        return;
    }
    //else if ... more types

这就是我所拥有的 API 类型,其中 A 和 B 派生自通用类型,我也可以测试该类型,但我需要派生类型:

...
else if (fieldInfo.FieldType == typeof(APITypeA))
{
    var val = (APITypeA)fieldInfo.GetValue(instance);
    val = MakeField<APITypeA>(label, val);  // My version, casts internally
    fieldInfo.SetValue(APITypeA, val);
    return;
}
else if (fieldInfo.FieldType == typeof(APITypeB))
{
    var val = (APITypeB)fieldInfo.GetValue(instance);
    val = MakeField<APITypeB>(label, val);  // My version, casts internally
    fieldInfo.SetValue(APITypeB, val);
    return;
}
...

这一切都有效,但我还有大约 10 个复制和粘贴代码其他的块“API 类型”。如果我不一般这样做,我只需传入从 API 的基本类型派生的任何类型,它就可以工作,但我不知道如何获取 fieldInfo 的实际类型。如果我打印它,它将打印派生类型的名称,但我似乎无法将它们打印出来。

我希望我能很好地阐述我的问题。这是高度情境化的,但似乎应该是可能的。如果我能得到 FieldType() 打印的类型,我就会很开心。否则我有 #regions 充满 else if 语句,这当然不像我想要的那么通用!

I'm writing a generic wrapper around a software's GUI API which has the ability to process quite a few of its own 'built-in' types but I can't figure out how to get it what it needs. For example, I'm doing this to handle strings:

("MakeTextField" and "MakeField" are pseudo-code, which take a value, display a GUI element and then return the value from the GUI for storage)

public static void FieldInfoMakeField<T>(T instance, System.Reflection.FieldInfo fieldInfo)
{
    string label = fieldInfo.Name;

    if (fieldInfo.FieldType == typeof(string))
    {
        var val = (string)fieldInfo.GetValue(instance);
        val = MakeTextField(label, val);
        fieldInfo.SetValue(instance, val);
        return;
    }
    //else if ... more types

This is what I have for the API's type where A and B are derrived from a common type, which I can also test for, but I need the derrived type:

...
else if (fieldInfo.FieldType == typeof(APITypeA))
{
    var val = (APITypeA)fieldInfo.GetValue(instance);
    val = MakeField<APITypeA>(label, val);  // My version, casts internally
    fieldInfo.SetValue(APITypeA, val);
    return;
}
else if (fieldInfo.FieldType == typeof(APITypeB))
{
    var val = (APITypeB)fieldInfo.GetValue(instance);
    val = MakeField<APITypeB>(label, val);  // My version, casts internally
    fieldInfo.SetValue(APITypeB, val);
    return;
}
...

This all works, but I have about 10 more copy&paste code blocks for other "APITypes". If I wasn't doing this generically, I would just pass in any type derived from the API's base type and it would work, but I can't figure out how to get the fieldInfo's actual type. If I print it, it will print the name of the derived types, but I can't seem to get them out.

I hope I'm stating my problem well enough. This is highly situational but seems like it should be possible. If I could just get the type that FieldType() prints, I would be gold. Otherwise I have #regions full of else if statements, which certainly isn't as generic as I would like!

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

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

发布评论

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

评论(1

浮萍、无处依 2024-12-13 08:10:11

您是否能够向 FieldInfoMakeField 函数提供字段的 Type

public static void FieldInfoMakeField<T, V>(T instance, FieldInfo fieldInfo)
{
    string label = fieldInfo.Name;

    var val = (V)fieldInfo.GetValue(instance);
    val = MakeField<V>(label, val);
    fieldInfo.SetValue(instance, val);
    return;
}

Are you able to provide the Type of the field to the FieldInfoMakeField function?

public static void FieldInfoMakeField<T, V>(T instance, FieldInfo fieldInfo)
{
    string label = fieldInfo.Name;

    var val = (V)fieldInfo.GetValue(instance);
    val = MakeField<V>(label, val);
    fieldInfo.SetValue(instance, val);
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文