1

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...

klashar
  • 2,519
  • 2
  • 28
  • 38
Alex
  • 1,013
  • 1
  • 13
  • 27
  • 5
    You want to take a feature that was created so that you can have strong compile-time guarantees of name matches, rather than relying on the brittle use of strings and... re-introduce a brittle use of strings? – Damien_The_Unbeliever Feb 22 '17 at 15:28
  • `nameof` was designed specifically for using the current name of the class, variable, etc. The intent was that if a name was changed, programmers wouldn't have to change hard-coded, literal text everywhere. – ps2goat Feb 22 '17 at 15:29
  • 1
    Agreed, question feels a bit odd, perhaps if you explain what you're ultimately trying to accomplish, maybe there is just a more practical way to achieve it. – Jake Feb 22 '17 at 15:30
  • 1
    This seems like a classic [XY Problem](http://meta.stackexchange.com/a/66378) scenario – Obsidian Phoenix Feb 22 '17 at 15:33
  • You're not making things much clearer with your edits. I can't understand what "the problem" is by just being shown a chunk of code. Similarly, what "fixing for that" even means when it's just another chunk of code isn't clear. I'd suggest you read some of the resources already linked here and also about [mcve]. – Damien_The_Unbeliever Feb 22 '17 at 18:51

2 Answers2

3

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;
Cù Đức Hiếu
  • 5,569
  • 4
  • 27
  • 35
1

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) // ...
Mikolaj
  • 1,231
  • 1
  • 16
  • 34
  • It's also possible to use `typeof(...).GetCustomAttribute()` to get attribute of given type. Useful when several attributes are defined – JL0PD Feb 24 '21 at 14:35