Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get properties of one class without custom class types

I have a Person class :

public class Person 
{
    virtual public long Code { get; set; }
    virtual public string Title { get; set; }       
    virtual public Employee Employee { get; set; }
}

I need to a generic solution for get all properties of Person class without properties by custom class types. Means select Code , Title properties.

typeof(Person).GetProperties();           //Title , Code , Employee
typeof(Person).GetProperties().Where(x => !x.PropertyType.IsClass); // Code

How can I select all properties without custom class types? (Code , Title)

like image 321
Ehsan Avatar asked Oct 07 '22 21:10

Ehsan


1 Answers

One way is to check the ScopeName of the Module of the Type:

typeof(Person).GetProperties().Where(x => x.PropertyType.Module.ScopeName == "CommonLanguageRuntimeLibrary")

since there is no direct way that to tell if a type is built-in or not.

To get some other ideas, see here, here and here.

like image 64
sloth Avatar answered Oct 10 '22 03:10

sloth