Alex
Alex

Reputation: 1160

How to change class/property name?

For example:

public class Car{

     public string color {get; set;}

     public int VINCode {get;set;}

}

Now if I call nameof(Car) it returns "Car"

[Name("something")]
public class Car{

     [Name("something_else")]
     public string color {get; set;}

     public int VINCode {get;set;}

}

But how can I get nameof to return the value in the Name attribute rather than the name of the class or method. eg: nameof(Car) == "something" or nameof(Car.color) == "something_else".

the problem:

var modelState = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(data);

        var departmentViewModels = modelState[nameof(DepartmentListView.DepartmentsViewModels)][0];
        var departmentTypes = modelState[nameof(DepartmentListView.DepartmentsViewModels)][0];

fixing for that:

var modelState = JsonConvert.DeserializeObject<DepartmentListView>(data);

        var departmentViewModels = modelState.DepartmentsViewModels;
        var departmentTypes = modelState.DepartmentTypes;

Serialization of this:

public class DepartmentListView
    {
        public IEnumerable<DepartmentViewModel> DepartmentsViewModels { get; set; }

        public IEnumerable<DepartmentType> DepartmentTypes { get; set; }
    }

will be:

departmentsViewModel : [], departmentTypes : [] (with lowercase)

I know I can change that lowercase serialization with JsonProperty, but I thought that I will can change the name of class or property...

Upvotes: 1

Views: 11742

Answers (2)

Mikolaj
Mikolaj

Reputation: 1869

It's look like you are asking about your attempted solution rather than your actual problem.

The solution to get new name of class / property:

Create your class with DisplayNameAttribute as follow:

[DisplayName("something")]
public class Car
{
    [DisplayName("something_else")]
    public string color { get; set; }

    public int VINCode { get; set; }
}

To get your class attribute name:

var attributes = typeof(Car) // type declaration
        
        .CustomAttributes.FirstOrDefault() // first item of collection that contains this member's custom attributes like [DisplayName("something")]
        
        ?.ConstructorArguments.FirstOrDefault() // first item of structure collecion
        
        .Value; // value as string - "something"

Similarly to the property name, you only need to get its type first

var attribute = typeof(Car).GetProperty(nameof(Car.color)) // ...

instead of

var attribute = typeof(Car) // ...

Upvotes: 1

Harry
Harry

Reputation: 5707

I'm afraid you cannot do it with nameof.

But if you want to get the value of a CustomAttribute you can try this:

// I assume this is your Name class
public class Name: Attribute
{
    public string Data { get; }
    public Name(string data) { Data = data; }
}

Then you can

// Will return "something"
var classAttrData = ((Name) typeof(Car).GetCustomAttribute(typeof(Name))).Data;

// Will return "something_else"
var fieldAttrData = ((Name) typeof(Car).GetField(nameof(Car.color)).GetCustomAttribute(typeof(Name))).Data;

Upvotes: 3

Related Questions