How do I tell if a type is a “simple” type? i.e. holds a single value

后端 未结 7 1960
北荒
北荒 2020-12-01 02:46
typeof(string).IsPrimitive == false
typeof(int).IsPrimitive == true
typeof(MyClass).IsClass == true
typeof(string).IsClass == true
typeof(string).IsByRef == false
ty         


        
7条回答
  •  孤城傲影
    2020-12-01 02:58

    It may not matter, but it sounds like you're leaving out a few cases:

    1. Complex types which have conversions
    2. Value types which don't have a parameterless constructor. Example below:

    There are probably more, but I think you're partitioning the problem space in an overly-restrictive manner.

     public class Person {
        private string _name;
        private int _age;
        public Person(string name, int age) {_name = name; _age = age;}
        // Remainder of value implementation
     }
    

提交回复
热议问题