Detecting if class property is a reference type

前端 未结 7 615
广开言路
广开言路 2020-12-09 14:44

Is it possible when looking at a class\' properties to detect if any of them is a reference type.

Take below as an example:

public class Client
{
            


        
相关标签:
7条回答
  • 2020-12-09 15:44

    All properties in your example return objects, as everything is an object in .NET; int and bool are objects. If you mean a reference type, as opposed to value types, then you can do the following:

    foreach (PropertyInfo pi in typeof(Client).GetProperties()) {
        if (pi.PropertyType.IsClass) {
            // reference type
            // DoMyFunkyStuff
        }
    }
    
    0 讨论(0)
提交回复
热议问题