Reset all values ​​from C # objects with Reflection

I have the following method, which is used to extract all values ​​as strings from an object using reflection. An object can have IEnumerables inside them, and I also want to get these values. You must also consider the list of ignore fields so that these field values ​​are not returned.

public static IEnumerable<string> StringContent(this object obj, IEnumerable<string> ignoreProperties = null) { Type t = obj.GetType(); foreach (var prop in t.GetProperties()) { if (ignoreProperties != null && ignoreProperties.Contains(field.Name)) { continue; } var value = prop.GetValue(obj); if (value != null) { if (value is IEnumerable<object>) { foreach (var item in (IEnumerable<object>)value) { foreach (var subValue in item.StringContent()) { yield return subValue.ToString(); } } } else { yield return value.ToString(); } } } } 

This method works great and gives the correct result. However, I need to speed it up as much as possible, because it does so many times.

Does anyone have any suggestions?

Thanks in advance!

** EDIT **

Test Case Example:

 [TestMethod] public void StringContent() { Project project = projectA; List<string> ignoreFields = new List<string>() { "SalesEngineer", "CreationDate" }; var result = project.StringContent(ignoreFields); Assert.IsTrue(result.Count() == 26); } 

Project Object:

 public class Project : IEntity { public Project() { Products = new List<ProjectProducts>(); } public int Id { get; set; } public DateTime CreationDate { get; set; } public string SalesEngineer { get; set; } public string SalesEngineerEmail { get; set; } public int? TeamId { get; set; } public string Name { get; set; } public string City { get; set; } public string Originator { get; set; } public string ContactName { get; set; } public string MainClient { get; set; } public string Contractor { get; set; } public string ContractorContactName { get; set; } public string ContractorLocation { get; set; } public string Wholesaler { get; set; } public string WholesalerContactName { get; set; } public string WholesalerLocation { get; set; } public float EstimatedValue { get; set; } public float CalculatedValue { get { return EstimatedValue/Convert.ToSingle(Currency != null ? Currency.Rate : (decimal)1.0); } } public int Probability { get; set; } public int SectorId { get; set; } public int TypeId { get; set; } public string StatusCode { get; set; } public string LostTo { get; set; } public int ReasonId { get; set; } public string SecondaryEngineer { get; set; } public float SplitPercentage { get; set; } public DateTime ExpectedOrder { get; set; } public DateTime? RequiredOnSiteBy { get; set; } public bool LightingDesignRequired { get; set; } public string ReasonForLightingDesign { get; set; } public DateTime? DesignRequiredBy { get; set; } public DateTime? FollowUp { get; set; } public string Comments { get; set; } public int CurrencyId { get; set; } public bool Void { get; set; } public string AttachmentFolder { get; set; } public virtual Currency Currency { get; set; } public virtual Reason Reason { get; set; } public virtual ProjectStatus Status { get; set; } public virtual ProjectType Type { get; set; } public virtual Sector Sector { get; set; } public virtual ICollection<ProjectProducts> Products { get; set; } public virtual Team Team { get; set; } public object Key { get { return Id; } } } 
+5
source share
1 answer

You can use stringify .

It exists in Nuget.

You can hide the parameters using the Hidden attribute.

You can print each object using a.stringify();

+2
source

Source: https://habr.com/ru/post/1270848/


All Articles