相关疑难解决方法(0)

如何遍历类的所有属性?

我上课了.

Public Class Foo
    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _Age As String
    Public Property Age() As String
        Get
            Return _Age
        End Get
        Set(ByVal value As String)
            _Age = value
        End Set
    End Property

    Private _ContactNumber As String
    Public Property ContactNumber() As String
        Get
            Return _ContactNumber
        End Get
        Set(ByVal value As String)
            _ContactNumber = value
        End Set
    End Property


End …
Run Code Online (Sandbox Code Playgroud)

.net vb.net reflection properties class

165
推荐指数
4
解决办法
15万
查看次数

是否有一个函数来检查对象是否是内置数据类型?

我想看看一个对象是否是C#中的内置数据类型

如果可能的话,我不想检查所有这些.
也就是说,我希望这样做:

        Object foo = 3;
        Type type_of_foo = foo.GetType();
        if (type_of_foo == typeof(string))
        {
            ...
        }
        else if (type_of_foo == typeof(int))
        {
            ...
        }
        ...
Run Code Online (Sandbox Code Playgroud)

更新

我试图递归创建一个PropertyDescriptorCollection,其中PropertyDescriptor类型可能不是内置值.所以我想做这样的事情(注意:这还不行,但我正在努力):

    public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        PropertyDescriptorCollection cols = base.GetProperties(attributes);

        List<PropertyDescriptor> list_of_properties_desc = CreatePDList(cols);
        return new PropertyDescriptorCollection(list_of_properties_desc.ToArray());
    }

    private List<PropertyDescriptor> CreatePDList(PropertyDescriptorCollection dpCollection)
    {
        List<PropertyDescriptor> list_of_properties_desc = new List<PropertyDescriptor>();
        foreach (PropertyDescriptor pd in dpCollection)
        {
            if (IsBulitin(pd.PropertyType))
            {
                list_of_properties_desc.Add(pd);
            }
            else
            {
                list_of_properties_desc.AddRange(CreatePDList(pd.GetChildProperties()));
            }
        }
        return list_of_properties_desc;
    }

    // …
Run Code Online (Sandbox Code Playgroud)

c# types built-in built-in-types

5
推荐指数
2
解决办法
3150
查看次数

标签 统计

.net ×1

built-in ×1

built-in-types ×1

c# ×1

class ×1

properties ×1

reflection ×1

types ×1

vb.net ×1