如何更改 class/property 名称?
How to change class/property name?
例如:
public class Car{
public string color {get; set;}
public int VINCode {get;set;}
}
现在如果我调用 nameof(Car)
它 returns "Car"
[Name("something")]
public class Car{
[Name("something_else")]
public string color {get; set;}
public int VINCode {get;set;}
}
但是我怎样才能让 nameof 成为 return Name 属性中的值而不是 class 或方法的名称。例如:nameof(Car) == "something"
或 nameof(Car.color) == "something_else"
.
问题:
var modelState = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(data);
var departmentViewModels = modelState[nameof(DepartmentListView.DepartmentsViewModels)][0];
var departmentTypes = modelState[nameof(DepartmentListView.DepartmentsViewModels)][0];
修复:
var modelState = JsonConvert.DeserializeObject<DepartmentListView>(data);
var departmentViewModels = modelState.DepartmentsViewModels;
var departmentTypes = modelState.DepartmentTypes;
序列化:
public class DepartmentListView
{
public IEnumerable<DepartmentViewModel> DepartmentsViewModels { get; set; }
public IEnumerable<DepartmentType> DepartmentTypes { get; set; }
}
将是:
departmentsViewModel : [], departmentTypes : [] (with lowercase)
我知道我可以使用 JsonProperty 更改小写序列化,但我认为我可以更改 class 或 属性 的名称。 .
恐怕你无法用 nameof
做到这一点。
但是如果你想得到一个 CustomAttribute
的值,你可以试试这个:
// I assume this is your Name class
public class Name: Attribute
{
public string Data { get; }
public Name(string data) { Data = data; }
}
那你可以
// 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;
您似乎在询问您尝试过的解决方案,而不是您实际遇到的问题。
class/属性新名字的解决方法:
使用 DisplayNameAttribute 创建您的 class,如下所示:
[DisplayName("something")]
public class Car
{
[DisplayName("something_else")]
public string color { get; set; }
public int VINCode { get; set; }
}
获取您的 class 属性名称:
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"
与属性名称类似,只需要先获取其类型
var attribute = typeof(Car).GetProperty(nameof(Car.color)) // ...
而不是
var attribute = typeof(Car) // ...
例如:
public class Car{
public string color {get; set;}
public int VINCode {get;set;}
}
现在如果我调用 nameof(Car)
它 returns "Car"
[Name("something")]
public class Car{
[Name("something_else")]
public string color {get; set;}
public int VINCode {get;set;}
}
但是我怎样才能让 nameof 成为 return Name 属性中的值而不是 class 或方法的名称。例如:nameof(Car) == "something"
或 nameof(Car.color) == "something_else"
.
问题:
var modelState = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(data);
var departmentViewModels = modelState[nameof(DepartmentListView.DepartmentsViewModels)][0];
var departmentTypes = modelState[nameof(DepartmentListView.DepartmentsViewModels)][0];
修复:
var modelState = JsonConvert.DeserializeObject<DepartmentListView>(data);
var departmentViewModels = modelState.DepartmentsViewModels;
var departmentTypes = modelState.DepartmentTypes;
序列化:
public class DepartmentListView
{
public IEnumerable<DepartmentViewModel> DepartmentsViewModels { get; set; }
public IEnumerable<DepartmentType> DepartmentTypes { get; set; }
}
将是:
departmentsViewModel : [], departmentTypes : [] (with lowercase)
我知道我可以使用 JsonProperty 更改小写序列化,但我认为我可以更改 class 或 属性 的名称。 .
恐怕你无法用 nameof
做到这一点。
但是如果你想得到一个 CustomAttribute
的值,你可以试试这个:
// I assume this is your Name class
public class Name: Attribute
{
public string Data { get; }
public Name(string data) { Data = data; }
}
那你可以
// 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;
您似乎在询问您尝试过的解决方案,而不是您实际遇到的问题。
class/属性新名字的解决方法:
使用 DisplayNameAttribute 创建您的 class,如下所示:
[DisplayName("something")]
public class Car
{
[DisplayName("something_else")]
public string color { get; set; }
public int VINCode { get; set; }
}
获取您的 class 属性名称:
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"
与属性名称类似,只需要先获取其类型
var attribute = typeof(Car).GetProperty(nameof(Car.color)) // ...
而不是
var attribute = typeof(Car) // ...