Hi,
Kindly help. I need a function which will contain a class object and it will return a dictionary as key value pair where key will be the name of the property and value will be the values of the property. that means :- DictionaryObject<classname.propertyname, property value>. It is easily possible using reflection. But the thing is that ,there can be a nested level of properties of various types like int,string,collection, property as collection, property as class type.
The function may also contain an entity class also with various level of nesting.If there is another way without using reflection that would be great because reflection does not work well for entitity framework and nestlings....
Your help is highly desired.
Regards,
Avisek Sarkar
What I have tried:
Our function written in class like :-
public class GetAllProperties
{
private static Dictionary<string, object> result = new Dictionary<string, object>();
private static bool IsSimpleType(Type type)
{
return
type.IsValueType ||
type.IsPrimitive ||
new Type[]
{
typeof(String),
typeof(Decimal),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
typeof(Guid)
}.Contains(type) ||
Convert.GetTypeCode(type) != TypeCode.Object;
}
public static Dictionary<string, object> GetProperties(object obj)
{
string ObjName = obj.GetType().Name;
var PropertyList = GetProperties(obj, ObjName);
return PropertyList;
}
private static Dictionary<string, object> GetProperties(object obj, string objName)
{
if (obj == null)
return result;
string ObjTypeName = objName;
Type objType = obj.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(obj, null);
if (IsSimpleType(property.PropertyType))
{
if (!CheckDuplicateKeyInDictionary(ObjTypeName + "." + property.Name))
{
result.Add(ObjTypeName + "." + property.Name, value: propValue);
}
}
else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
try
{
if (property.PropertyType == typeof(string[]))
{
if (!CheckDuplicateKeyInDictionary(ObjTypeName + "." + property.Name))
{
result.Add(ObjTypeName + "." + property.Name, value: Convert.ToString(propValue));
}
}
else
{
IEnumerable enumerable = (IEnumerable)propValue;
if (enumerable != null)
{
object enumTypeValue = enumerable.GetEnumerator().GetType();
if (!CheckDuplicateKeyInDictionary(ObjTypeName + "." + property.Name))
{
result.Add(ObjTypeName + "." + property.Name, value: enumTypeValue);
}
foreach (object child in enumerable)
{
ObjTypeName = ObjTypeName + "." + property.Name + "." + child.GetType().Name;
GetProperties(child, ObjTypeName);
}
}
else
{
if (!CheckDuplicateKeyInDictionary(ObjTypeName + "." + property.Name))
{
result.Add(ObjTypeName + "." + property.Name, value: null);
}
}
}
}
catch (Exception)
{
throw;
}
}
else
{
if (propValue == null)
{
GetProperties(property.PropertyType, ObjTypeName + "." + property.Name);
}
else
{
ObjTypeName = ObjTypeName + "." + propValue.GetType().Name;
Object objTypeValue = (Object)propValue;
object ObjValue = objTypeValue.GetType();
if (ObjValue != null)
{
if (!CheckDuplicateKeyInDictionary(ObjTypeName + "." + property.Name))
{
result.Add(ObjTypeName + "." + property.Name, value: ObjValue);
}
GetProperties(propValue, ObjTypeName + "." + property.Name);
}
}
}
}
return result;
}
private static Dictionary<string, object> GetProperties(Type objType, string objName)
{
string ObjTypeName = objName;
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = null;
if (IsSimpleType(property.PropertyType))
{
result.Add(ObjTypeName + "." + property.Name, value: propValue);
}
else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
var enumtype = typeof(IEnumerable).IsAssignableFrom(property.PropertyType).GetType();
if (property.PropertyType == typeof(string[]))
{
result.Add(ObjTypeName + "." + property.Name, value: propValue);
}
else
{
result.Add(ObjTypeName + "." + property.Name, value: null);
GetProperties(property.PropertyType, objName + "." + property.Name);
}
}
else
{
GetProperties(property.PropertyType, ObjTypeName + "." + property.Name);
}
}
return result;
}
private static bool CheckDuplicateKeyInDictionary(string keyName)
{
if (result.ContainsKey(keyName))
{
return true;
}
else
return false;
}
}