存储字符串中指定类型的属性

发布于 2024-11-25 13:24:41 字数 1804 浏览 0 评论 0原文

有一个 XML 方案,其内容如下:

<ExtraFields>
  <ExtraField Type="Int">
   <Key>Mileage</Key>
   <Value>500000 </Value>
  </ExtraField>
  <ExtraField Type="String">
   <Key>CarModel</Key>
   <Value>BMW</Value>
  </ExtraField>
  <ExtraField Type="Bool">
   <Key>HasAbs</Key>
   <Value>True</Value>
  </ExtraField>    
</ExtraFields>

我想将此信息存储在类中,并且我希望其字段具有指定的类型。我想到了一种通用方法

     static class Consts
{
    public const string Int32Type = "int32";
    public const string StringType = "string";
    public const string BoolType = "bool";
}

public class ExtraFieldValue<TValue>
{
    public string Key;
    public TValue Value;public static ExtraFieldValue<TValue> CreateExtraField(string strType, string strValue, string strKey)
    {
        IDictionary<string, Func<string, object>> valueConvertors = new Dictionary<string, Func<string, object>> {
                  { Consts.Int32Type, value => Convert.ToInt32(value)},
                  { Consts.StringType, value => Convert.ToString(value)},
                  { Consts.BoolType, value => Convert.ToBoolean(value)}
        };

        if (!valueConvertors.ContainsKey(strType))
            return null;

        ExtraFieldValue<TValue> result = new ExtraFieldValue<TValue>
        {
            Key = strKey,
            Value = (TValue)valueConvertors[strType](strValue)
        };

        return result;
    }

}

,但这种方法的问题是我需要一个 ExtraFields 列表,并且每个字段在列表中可以有不同的类型。

到目前为止,我只能想到两个选项:

1)为此字段使用动态关键字,但这种方法似乎有限制

2)为该字段使用对象类型并将其动态类型转换为必要的类型。但无论如何,如果我需要一些特定于对象的调用,我将必须进行静态转换。

我很高兴阅读您的想法/建议

There's an XML scheme which says something like this:

<ExtraFields>
  <ExtraField Type="Int">
   <Key>Mileage</Key>
   <Value>500000 </Value>
  </ExtraField>
  <ExtraField Type="String">
   <Key>CarModel</Key>
   <Value>BMW</Value>
  </ExtraField>
  <ExtraField Type="Bool">
   <Key>HasAbs</Key>
   <Value>True</Value>
  </ExtraField>    
</ExtraFields>

I want to store this info in the class and I want its field to be of the specified type. I thought of a generic approach

     static class Consts
{
    public const string Int32Type = "int32";
    public const string StringType = "string";
    public const string BoolType = "bool";
}

public class ExtraFieldValue<TValue>
{
    public string Key;
    public TValue Value;public static ExtraFieldValue<TValue> CreateExtraField(string strType, string strValue, string strKey)
    {
        IDictionary<string, Func<string, object>> valueConvertors = new Dictionary<string, Func<string, object>> {
                  { Consts.Int32Type, value => Convert.ToInt32(value)},
                  { Consts.StringType, value => Convert.ToString(value)},
                  { Consts.BoolType, value => Convert.ToBoolean(value)}
        };

        if (!valueConvertors.ContainsKey(strType))
            return null;

        ExtraFieldValue<TValue> result = new ExtraFieldValue<TValue>
        {
            Key = strKey,
            Value = (TValue)valueConvertors[strType](strValue)
        };

        return result;
    }

}

But the problem with this approach is that I need a list of ExtraFields and each of them can have different type in a list.

I can only think of two options so far:

1) using a dynamic keyword for this field but this approach seems to have limits

2) using an object type for the field and casting its dynamic type to the necessary type. But anyhow if I will need some object specific calls, I will have to make a static cast.

I would be glad to read your thoughts/proposals

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

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

发布评论

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

评论(1

情独悲 2024-12-02 13:24:41

只需使用名称/值集合。如果您直到运行时才知道属性名称,那么使用动态或在运行时动态构建类型对您没有帮助,因为您将无法编写访问这些属性的源代码特性。

因此,只需使用名称/值集合,例如实现 IDictionary的集合。

Just use a name/value collection. If you don't even know the property names until runtime, using dynamic, or dynamically building a type at runtime isn't going to help you because you won't be able to write source that accesses those properties.

So, just use a name/value collection like something that implements IDictionary<string, object>.

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