从Type中获取属性的默认值

时间:2017-10-06 15:44:18

标签: c# asp.net asp.net-mvc oop properties

我试图从它的类型中获取属性的默认值。

但我收到错误(在我的代码中发表评论)。

在这方面花了一些时间安静,任何帮助都将不胜感激,谢谢。

这是代码的简化版本。

    public void test<T>(T classObject)
    {
        Type t = typeof(T);

        var properties = t.GetProperties(BindingFlags.Public);


        foreach (var prop in properties)
        {
            var type = prop.GetType();

            var defaultValue = default(type); //Get an error here saying "the type or namespace name "type" could not be found
        }
    }

0 个答案:

没有答案