获得“基本”数据类型,而不是奇怪的可为空的数据类型,通过 C# 中的反射

发布于 2024-10-20 04:19:16 字数 1198 浏览 0 评论 0原文

我的基本需求是从 LINQ to SQL 查询生成的匿名类型中获取数据类型。

我有一段代码(比我能写的更聪明,因为我还没有真正深入研究反射),它从匿名类型返回数据类型,并且非常适合 linq2sql 属性中标记为“不可为空”的元素。因此,如果我有一个字符串,它将作为 System.String 返回。但是,当元素可为空时,我最终得到它的“全名”:

{Name = "Nullable1" FullName = "System.Nullable1[[System.Decimal, mscorlib, Version =4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

在这种情况下,我想要提取的是 System.Decimal 类型(在字符串或其他情况下,我只想要 System.String)。我查看了所有属性,但找不到任何似乎可以存储此内容的内容。

    private static Dictionary<string, Type> GetFieldsForType<T>(IEnumerable<T> data)
    {
        object o = data.First();

        var properties = o.GetType().GetProperties();

        return properties.ToDictionary(property => property.Name, property => property.PropertyType);
    }

LINQ 查询(我认为这里并不重要):

var theData = from r in db.tblTestEdits select new { myitem = r.textField, mydecimal = r.decimalField }; 

我发现这个链接似乎试图解决类似的问题。
http://ysgitdiary.blogspot.com/2010/02/blog-post.html

虽然它似乎返回类型的“字符串”而不是实际类型,但这正是我需要的。不知道如何转换这样的东西。

非常感谢大家。

My basic need is to get the datatypes from an anonymous type generated from a LINQ to SQL query.

I have a piece of code (cleverer than I could write, since I haven't really delved into reflection) which returns the datatypes from an anonymous types, and works perfectly for the elements marked 'not nullable' in the linq2sql properties. So, if I have a string it will return as System.String. However, when the element is nullable I end up with the 'full name' of it being:

{Name = "Nullable1" FullName = "System.Nullable1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

All I want to extract is the System.Decimal type in such a case (and in cases of strings or whatever, I'd just want System.String). I've looked through the properties and can't find anything that seems to store this.

    private static Dictionary<string, Type> GetFieldsForType<T>(IEnumerable<T> data)
    {
        object o = data.First();

        var properties = o.GetType().GetProperties();

        return properties.ToDictionary(property => property.Name, property => property.PropertyType);
    }

The LINQ query (which I don't think really matters here):

var theData = from r in db.tblTestEdits select new { myitem = r.textField, mydecimal = r.decimalField }; 

I found this link that seems to try to address a similar thing.
http://ysgitdiary.blogspot.com/2010/02/blog-post.html

Though it seems to return a "string" of what the type is rather than the actual type, which is what I need. Not sure how to convert such a thing.

Many thanks all.

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

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

发布评论

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

评论(2

开始看清了 2024-10-27 04:19:16
private static Type GetCoreType(Type type)
{
    if (type.IsGenericType &&
        type.GetGenericTypeDefinition() == typeof(Nullable<>))
        return Nullable.GetUnderlyingType(type);
    else
        return type;
}
private static Type GetCoreType(Type type)
{
    if (type.IsGenericType &&
        type.GetGenericTypeDefinition() == typeof(Nullable<>))
        return Nullable.GetUnderlyingType(type);
    else
        return type;
}
神回复 2024-10-27 04:19:16

你可能想要这样的东西

        Type targetType;
        bool isNullable;

        // Do we have a nullable type?
        if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
        {
            isNullable = true;
            targetType = type.GetGenericArguments()[0];
        }
        else
        {
            isNullable = false;
            targetType = type;
        }

You want something like this maybe

        Type targetType;
        bool isNullable;

        // Do we have a nullable type?
        if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
        {
            isNullable = true;
            targetType = type.GetGenericArguments()[0];
        }
        else
        {
            isNullable = false;
            targetType = type;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文