Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip all values from C# objects using Reflection

Tags:

c#

reflection

I have the following method which is used to retrieve all values as strings from an object using reflection. The object can have IEnumerables within them and I also want to retrieve these values. A list of ignore fields also needs to be taken into account so that those field's 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 does work perfectly and gives me the correct result. However, I need to speed it up as much as possible because this is performed a lot of times.

Does anybody have any suggestions?

Thanks in advance!

** EDIT **

Example Test Case:

[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; }
    }

}
like image 883
DaRoGa Avatar asked Oct 29 '22 03:10

DaRoGa


1 Answers

You can use stringify package.

It exists in Nuget.

You can Hide parameters with Hidden attribute.

You can print every object with a.stringify();

like image 188
Ygalbel Avatar answered Nov 15 '22 06:11

Ygalbel