-2

I have a problem fetching object from the array object that I made. It seems it didn't fetch the object see my code below:

Product Model

public class Product
{
    public string Id { get; set; }
    public List<ExcelName> ShortDesc { get; set; } // I want to get the object from here
}

Short Description Model

// get this object and the properties inside it.
public class ExcelName
{
    public string Name { get; set; }
    public string Language { get; set; }
}

My Code

private static T SetValue<T>(Dictionary<string, object> objectValues)
{
    var type = typeof(T);
    var objInstance = Activator.CreateInstance(type);
    if (!type.IsClass) return default;
    foreach (var value in objectValues)
    {
         if (value.Key.Contains(":Language="))
         {
             var propName = value.Key.Split(':')[0];
             // propName is ShortDesc object
             var propInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(e => e.Name.ToLower() == propName.ToLower().Replace(" ", ""));
             if (propInfo == null) continue;
             if (propInfo.PropertyType.IsGenericType)
             {
                 // I want to get the type and properties from T generic using reflection instead static
                 var name = typeof(ExcelName);
                 var excelNameObjectInstance = Activator.CreateInstance(name);
                 foreach (var propertyInfo in name.GetProperties())
                 {
                     propertyInfo.SetValue(excelNameObjectInstance, value.Value, null);
                 }

                 // add excelNameObjectInstance object to the list in ShortDesc
              }
         }
    }

}

How to fetch the object from the list of ShortDesc to get the ExcelName objects.

Alvin Quezon
  • 1,089
  • 3
  • 11
  • 29
  • So... why are you creating types `Activator.CreateInstance` ? – TheGeneral Oct 12 '20 at 05:30
  • Also... What has `Dictionary objectValues` got to do with anything ? why is there a dictionary here? There is a lot in this code that doesn't fit the description – TheGeneral Oct 12 '20 at 05:31
  • Hi @MichaelRandall, `Dictionary objectValues` consist of the name of the property and the value to be set into the property. The problem is I can't get the object `ExcelName` and it's property to set the value. What I am trying earlier is to get the properties form the list. – Alvin Quezon Oct 12 '20 at 05:35
  • Note as well that I already got the object `ShortDesc` from the `Product` class though I can't proceed to fetch the object and properties from the `ExcelName` list. – Alvin Quezon Oct 12 '20 at 05:36
  • So you have a dictionary of properties you want to set, on the lists of ExcelNames in product ? – TheGeneral Oct 12 '20 at 05:37
  • @MichaelRandall Hmp, no, the properties are coming from the `T` generic. the only thing in the dictionary are the values and posible properties that could be same as the property name in generic `T`. – Alvin Quezon Oct 12 '20 at 05:40
  • Hi @MichaelRandall, I updated the question and my code please check. – Alvin Quezon Oct 12 '20 at 06:04

1 Answers1

0

I'm not quite sure what you're trying to do, but it seems like you want a function that instantiates a T and sets its properties according to a dictionary.

Half your code doesn't make sense to me, but my guess is correct, you shouldn't need anything more complicated than this:

private static T SetValue<T>(Dictionary<string, object> objectValues) where T : class, new()
{
    var type = typeof(T);
    var instance = new T();
    foreach (var entry in objectValues)
    {
        type.GetProperty(entry.Key).SetValue(instance, entry.Value);
    }
    return instance;
}

If you don't expect the keys to be an exact match for property names, you can introduce a normalization function:

private static string Normalize(string input)
{
    return input.ToLower().Replace(" ", "");
}

private static T SetValue<T>(Dictionary<string, object> objectValues) where T : class, new()
{
    var type = typeof(T);
    var instance = new T();
    foreach (var entry in objectValues)
    {
        var prop = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
            .First( x => Normalize(x.Name) == Normalize(entry.Key) );
        prop.SetValue(instance, entry.Value);
    }
    return instance;
}
John Wu
  • 50,556
  • 8
  • 44
  • 80