Populating custom class properties using Reflection - c#

First of all , I'm new to reflection . I've created class :
using System.Reflection;
public class EmployeeInfo
{
public string EmployeeName { get; set; }
public string PhoneNumber { get; set; }
public string Office { get; set; }
public string Department { get; set; }
public string Position { get; set; }
public string PhoneType { get; set; }
public bool IsPublic { get; set; }
}
Now I'm trying to develop a method that will populate all the properties using some business logic (if null then empty string etc) via reflection, and returning a list of EmployeeInfo. I thought it should look something like this :
public List<Models.EmployeeInfo> GetEmployeeInfo(SPListItemCollection splic)
{
var listEmployeeInfo = new List<Models.EmployeeInfo>();
var propertyNames = new List<string>() {"EmployeeName","Position","Office","IsPublic"};
foreach (SPListItem item in splic)
{
var employeeInfo = new Models.EmployeeInfo();
foreach (var propertyName in propertyNames)
{
string newData = "";
if (item[propertyName] != null)
{
newData = item[propertyName].ToString();
}
employeeInfo.GetProperty(propertyName).SetValue(employeeInfo, newData, null);
}
listEmployeeInfo.Add(employeeInfo);
}
return listEmployeeInfo;
}
But I can't call GetProperty or SetValue extension methods at this line :
employeeInfo.GetProperty(propertyName).SetValue(employeeInfo, newData, null);
Error Message says that my Models.EmployeeInfo class doesn't contain definition for GetProperty and no extension method GetProperty .
What is missing ?
Thank you .

GetProperty is a method on the Type class.
employeeInfo.GetType().GetProperty(propertyName).SetValue(employeeInfo, newData, null);

Related

How to compare object and List<> and object.value null when the value doesn't exist in list

I need to compare between my object and a list.
List contains attribute "nom_colonne", it fill by a query and the result is a list of attributes (same like my object AppareilsReparations)
For example :
if droit_utilisateur.nom_colonne = "Num_dossier"
so i keep the value in arp.num_dossier
But if i don't have this value of my list droit_utilisateur :
arp.num_dossier will be null.
I wanted to cast my object with System.Collections.IList but impossible to cast. I have an error.
public class AppareilsReparations
{
public string Num_dossier { get; set; }
//public string reference_aff { get; set; }
public int Id { get; set; }
public Guid Uid { get; set; }
public string ref_sav { get; set; }
public CodeDefaut codedefaut { get; set; }
public CodeSymptome codesymptome { get; set; }
}
public class Droits
{
public int id { get; set; }
public int utilisateur_id { get; set; }
public string nom_table { get; set; }
public string nom_colonne { get; set; }
}
AppareilsReparations arp = db.Query<AppareilsReparations>
("select * from v_appareils_reparations where ref_sav_client =#ref_sav", new { ref_sav }).SingleOrDefault();
List<Droits> droit_utilisateur = GetDroits("admin");
//var appareil = new List<AppareilsReparations>();
IList appareil = (IList)arp;
var result = droit_utilisateur.Where(x => !appareil.Contains(x.nom_colonne)).ToList();
That's cause AppareilsReparations is not a type of IList and thus your cast would always fail IList appareil = (IList)arp;. Probably you wanted to do like below; just comparing with Num_dossier field of AppareilsReparations type
var result = droit_utilisateur.Where(x => x.nom_colonne == arp.Num_dossier).ToList();
i have found my problem, i post my code for everybody :
List<Droits> droit_utilisateur = GetDroits(username);
Type myType = e.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in e.GetType().GetProperties())
{
object propValue = prop.Name;
if (droit_utilisateur.Any(s => s.nom_colonne.Contains(prop.Name/*, StringComparison.OrdinalIgnoreCase)*/) == false))
{
prop.SetValue(e, Convert.ChangeType(null, prop.PropertyType), null);
}
}
return e;

How to convert Generic.List values into extension method

I am using Linq to Entity , Linq query returning a ToList() and storing multiple rows in DataSourcesModel class. Later i am using foreach to iterate items and store values in DataSourcesModelDTO, till here working fine.
Now question is DataSourcesModelDTO dto storing only one row value.
I am expecting it should store multiple row value. How to achieve this in extension method?
** var datasoruceModeltDTO = DataSourcesDTOTransformers.ToDTO(result);**
public static class DataSourcesDTOTransformers
{
public static DataSourcesModelDTO ToDTO(this List<DataSourcesModel> model)
{
if (model == null) { return null; }
var dto = new DataSourcesModelDTO();
ToDTO(model, dto);
return dto;
}
public static void ToDTO(List<DataSourcesModel> model1, DataSourcesModelDTO dto)
{
foreach (var model in model1)
{
dto.DataSourceConfigID = model.DataSourceConfigID.ToString();
dto.DataSourceID = model.DataSourceID.ToString();
dto.Name = model.Name;
dto.server = model.server;
dto.LastModified = model.LastModified;
dto.Instance = model.Instance;
}
}
}
[DataContract]
public class DataSourcesModelDTO
{
[DataMember]
public string DataSourceID { get; set; }
[DataMember]
public string DataSourceConfigID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string server { get; set; }
[DataMember]
public string Instance { get; set; }
[DataMember]
public bool Deleted { get; set; }
[DataMember]
public DateTime LastModified { get; set; }
[DataMember]
public DateTime LastSync { get; set; }
}
foreach (var model in model1)
{
dto.DataSourceConfigID = model.DataSourceConfigID.ToString();
You are setting the properties of the same dto object every time in the loop. I would rearrange your toDTO methiod to either:
take in a single DataSourcesModel object and return a single DataSourcesModelDTO object (calling the method from within a loop or Linq query), or
take in an IEnumerable<DataSourcesModel> and return an IEnumerable<DataSourcesModelDTO>
How to achieve this in extension method?
Not sure why you need an extension method, but the appropriate signature would be:
public static IEnumerable<DataSourcesModelDTO> ToDTOCollection (this IEnumerable<DataSourcesModel> model)
I'll let you work out what the implementation would be. Hint: You could create a List<DataSourcesModelDTO> within the method and populate it with new objects the same way you are now.

Passing dynamic type, iterating fields and replacing values

I have 3/4 different models that each contain their own nested model. I need a way of iterating all fields, including those of the nested model and do a string replace (although not all fields are strings).
My initial idea was to write a method which allows for a 'dynamic' type to be passed.
Input model:
Name = Joe
Surname = Smith
Address = new ClientAddress
{
Line1: Item A
Line2: mistake
Line3: mistake
}
My example method:
MyMethod (dynamic passInModel)
{
....
passInModel.Replace("mistake","correction");
return passInModel;
}
Output:
Name = Joe
Surname = Smith
Address = new ClientAddress
{
Line1: Item A
Line2: correction
Line3: correction
}
Despite trying various ways of doing it I've not had any success in writing something that does the job.
You could write a method that accepts an object and use reflection to iterate through all the fields, but you're getting into messy territory there. In my opinion, even using dynamic here is messy.
Consider using a modified visitor pattern here. If your domain objects look like this:
public class ModelBase
{
}
public class MyModel1 : ModelBase
{
public string Name { get; set; }
public string Surname { get; set; }
public ClientAddress Address { get; set; }
}
public class MyModel2 : ModelBase
{
public string CompanyName { get; set; }
public string Region { get; set; }
public CompanyAddress Address { get; set; }
}
public class ClientAddress
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
}
public class CompanyAddress
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public List<string> AdditionalLines { get; set; }
}
Write a visitor that takes an abstract ModelBase and dispatches the correct type-safe visitor:
public class ModelFixVisitor
{
public ModelBase Visit(ModelBase model)
{
var asModel1 = model as MyModel1;
if (asModel1 != null)
{
return new Model1FixVisitor().Visit(asModel1);
}
var asModel2 = model as MyModel2;
if (asModel2 != null)
{
return new Model2FixVisitor().Visit(asModel2);
}
throw new NotImplementedException("Unknown model type.");
}
}
Then write a simple class for each type (and subtype) you need to visit:
public class Model1FixVisitor
{
public MyModel1 Visit(MyModel1 model)
{
model.Name = new StringFixVisitor().Visit(model.Name);
model.Surname = new StringFixVisitor().Visit(model.Surname);
model.Address = new ClientAddressFixVisitor().Visit(model.Address);
return model;
}
}
public class Model2FixVisitor
{
public MyModel2 Visit(MyModel2 model)
{
model.CompanyName = new StringFixVisitor().Visit(model.CompanyName);
model.Region = new StringFixVisitor().Visit(model.Region);
model.Address = new CompanyAddressFixVisitor().Visit(model.Address);
return model;
}
}
public class ClientAddressFixVisitor
{
public ClientAddress Visit(ClientAddress address)
{
address.Line1 = new StringFixVisitor().Visit(address.Line1);
address.Line2 = new StringFixVisitor().Visit(address.Line2);
address.Line3 = new StringFixVisitor().Visit(address.Line3);
return address;
}
}
public class CompanyAddressFixVisitor
{
public CompanyAddress Visit(CompanyAddress address)
{
address.Line1 = new StringFixVisitor().Visit(address.Line1);
address.Line2 = new StringFixVisitor().Visit(address.Line2);
address.AdditionalLines = new StringListFixVisitor().Visit(address.AdditionalLines);
return address;
}
}
public class StringFixVisitor
{
public string Visit(string element)
{
return element.Replace("mistake", "correction");
}
}
public class StringListFixVisitor
{
public List<string> Visit(List<string> elements)
{
return elements
.Select(x => new StringFixVisitor().Visit(x))
.ToList();
}
}
I'm sure the code could be refactored and optimized, but it should express the general idea.
What I like about this type of solution is that it breaks the problem down into small, manageable chunks: How do I fix a string? How do I fix a ClientAddress?
Fixing entire models then becomes simple composition of these smaller classes. It's a little more verbose, but you get to keep type safety, and don't have to mess with reflection.
You can use the power of .Net reflection to solve this.
I created a class called DeepStringReplacer. Using reflection it iterates through object properties and if the type is string, perform string replace.
Check the code below:
public class DeepStringReplacer
{
public object Replace(object input, string oldValue, string newValue)
{
if (input is string)
{
return input.ToString().Replace(oldValue, newValue);
}
var fields = input.GetType().GetProperties();
foreach (var field in fields)
{
var fieldValue = field.GetValue(input);
field.SetValue(input, Replace(fieldValue, oldValue, newValue));
}
return input;
}
}
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public ClientAddress Address { get; set; }
}
public class ClientAddress
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
}

Selecting property values from IEnumerable<t> using an expression list?

So I want to create a MVC helper where users can pass in a collection of expressions and the collection to pull those values from. This will create a table with the column Names and then a list of the values for those columns.
public class ColumnDefinition<T> where T: class
{
public Expression<Func<T,object>> Property { get; set; }
public string DisplayName { get; set; }
public string DefaultValue { get; set; }
public bool IsVisisble { get; set; }
public string CssClass { get; set; }
}
So the helper might look like this:
public static IHtmlString ToTable<T>(this HtmlHelper helper, IEnumerable<T> list, IEnumberable<ColumnDefinition<T>> columnDefs) where T: class
{
....
}
I know how to get the Property name fromthe Expression<Func<T,object>>, but not sure how to wrtiethe select statement for the list. this should work to get the values:
var someList= list.Select(() => columnDefs.Select(c => c.Property)).ToList();
I am trying to figure out how to Line up the Name with the value. So an exmaple might be this:
var colDef = new List<ColumnDefinition<Foo>>()
{
new ColumnDefinition<Foo>()
{
Property = f => f.Id,
DisplayName = "Foo"
},
new ColumnDefinition<Foo>()
{
Property = f => f.Bar.Name,
}
}
So when that set of column definitions gets passed into the helper, I'd like to Get all the property names unless the DisplayName is present (i understand how to get the names) then I want to write the data for each one of those column definitions.
Update
So I have this so far:
public static class DataTablesHelper
{
public static DataTableModel GnerateColumns<T>(IEnumerable<T> list,
IEnumerable<ColumnDefinition<T>> columnDefinitions) where T: class
{
foreach (var o in list)
{
var newList = GetInfo(o, columnDefinitions.ToList());
}
return new DataTableModel();
}
private static List<string> GetInfo<T>(T source, IEnumerable<ColumnDefinition<T>> columnDefinitions) where T : class
{
return columnDefinitions.Select(columnDefinition => columnDefinition.Property(source).ToString()).ToList();
}
}
public class ColumnDefinition<T> where T: class
{
public Func<T,object> Property { get; set; }
public string DisplayName { get; set; }
public string Value { get; set; }
public string SortValue { get; set; }
public bool IsVisisble { get; set; }
public string CssClass { get; set; }
}
This seems to work on getting the values and I can grab the Property names later. Ideally, it would be nice to pass in a string formatter to format the output.
Update 2
So, using this technique which has been part of my core library now for some time, I came up with this to allow for simple formatting:
private static List<string> GetInfo<T>(T source, IEnumerable<ColumnDefinition<T>> columnDefinitions) where T : class
{
var listValues = new List<string>();
foreach (var columnDefinition in columnDefinitions.ToList())
{
var prop = columnDefinition.Property(source);
var definition = columnDefinition;
TypeSwitch.Do(prop, TypeSwitch.Case<DateTime>(p => listValues.Add(p.ToString(definition.Format))),
TypeSwitch.Default(() => listValues.Add(prop.ToString())));
}
return listValues;
}
I added a string that will house the formatter.
public class ColumnDefinition<T> where T: class
{
public Func<T,object> Property { get; set; }
public string Format { get; set; }
public string DisplayName { get; set; }
public string Value { get; set; }
public string SortValue { get; set; }
public bool IsVisisble { get; set; }
public string CssClass { get; set; }
}

How to convert DataSet to Entity in C#?

Here I came up with the first approach: that is, using foreach clause on DataSet to get the items and fill in your Entity. But this method can't be reused when the something changes.
So I think maybe reflection should be the best approach for my scenario. below are the details:
1.My entity is defined below:
using System.ComponentModel;
namespace WarningServiceForYM.Model
{
public class StuffEntity
{
[Description("企业ID")]
public string EnterpriseID { get; set; }
[Description("冰柜编号")]
public string FridgeID { get; set; }
[Description("冰柜名称")]
public string FridgeName { get; set; }
[Description("手机号码")]
public string PhoneNumber { get; set; }
[Description("设备名称")]
public string EquipmentName { get; set; }
[Description("采集参数")]
public string PickingParams { get; set; }
[Description("一路温度")]
public string TempOne { get; set; }
[Description("二路温度")]
public string TempTwo { get; set; }
[Description("三路温度")]
public string TempThree { get; set; }
[Description("四路温度")]
public string TempFour { get; set; }
[Description("五路温度")]
public string TempFive { get; set; }
[Description("六路温度")]
public string TempSix { get; set; }
[Description("七路温度")]
public string TempSeven { get; set; }
[Description("八路温度")]
public string TempEight { get; set; }
[Description("温度最低")]
public string Min { get; set; }
[Description("温度最高")]
public string Max { get; set; }
[Description("采集时间")]
public string PickingTime { get; set; }
[Description("通知间隔")]
public string WarningPeriod { get; set; }
[Description("延时")]
public string PendingTime { get; set; }
[Description("通知开关")]
public string Switch { get; set; }
[Description("最后通知时间")]
public string LastInformTime { get; set; }
}
}
Below is the DataSet screenshot:
I have saved this DataSet's data into csv file, pls click here to find it.
the inner properties in the StuffEntity have the same Description as the column headers in DataSet.
Would any one give me a method to show how to convert this Dataset to StuffEntity ? thx.
Ok, using reflection:
public static T GetEntity<T>(DataRow row) where T : new()
{
var entity = new T();
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
//Get the description attribute
var descriptionAttribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), true).SingleOrDefault();
if (descriptionAttribute == null)
continue;
property.SetValue(entity, row[descriptionAttribute.Description]);
}
return entity;
}
You can use it like:
foreach (DataRow dataRow in dataSet.Tables[0].Rows)
{
var e = GetEntity<StuffEntity>(dataRow);
Console.WriteLine(e.EnterpriseID);
}
It's a generic implementation, so you can use it with any other types or datasets you want.
I took care to made it as simple as possible, so it can be widely improved adding some consistences, like checking if the column name exists before setting the entity value or verifying duplicate descriptions as needed. It can also be converted to an extension method for the DataRow, DataTable or DataSet, for example.
I my case Your code almost worked, but
descriptionAttribute is always null so it continues to the point where I get no results back
var descriptionAttribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), true).SingleOrDefault();
if (descriptionAttribute == null)
continue;
I am not sure what is happening to the variable descriptionAttribute and it is null all the time.
So I changed #natenho code like this and it worked for me.
public static T GetEntity<T>(DataRow row) where T : new()
{
var entity = new T();
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
//Get the description attribute
//var descriptionAttribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), true).SingleOrDefault();
//if (descriptionAttribute == null)
// continue;
if (row[property.Name].ToString() != "")
{
property.SetValue(entity, row[property.Name]);
}
}
return entity;
}

Categories