| name | Metadata from object |
| nuget |
https://www.nuget.org/packages/AOPMethodsCommon/ |
| link | http://msprogrammer.serviciipeweb.ro/category/roslyn/ |
| author | Andrei Ignat |
This will generate code to retrieve the values of properties directly,not by reflection
The code that you start with is
[AutoMethods(template = TemplateMethod.CustomTemplateFile,CustomTemplateFileName = "GenerateFromPOCO.txt")]
public partial class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
The code that you will use is
var p = new Person();
p.FirstName = "Andrei";
p.LastName = "Ignat";
var last = p.ValueProperty(Person_EnumProps.LastName);
var first = p.ValueProperty("FirstName");
Console.WriteLine(last + " "+first);
The code that is generated is
public enum Person_EnumProps{
None
,FirstName // Public
,LastName // Public
}
partial class Person{
public object ValueProperty(Person_EnumProps val){
if(val == Person_EnumProps.FirstName) {
return this.FirstName;
}
if(val == Person_EnumProps.LastName) {
return this.LastName;
}
throw new ArgumentException("cannot find "+ val);
}
public object ValueProperty(string val){
if(string.Compare("FirstName",val,StringComparison.CurrentCultureIgnoreCase)==0) {
return this.FirstName;
}
if(string.Compare("LastName",val,StringComparison.CurrentCultureIgnoreCase)==0) {
return this.LastName;
}
throw new ArgumentException("cannot find "+ val);
}
}
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/MetadataFromObject
Leave a Reply