Click here to Skip to main content
15,887,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :-

C#
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)) // if object type is of Enumeration type.

                {
                    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);
                                }

                                //TODO Get the type of the enum and iterate over to collect class propertes.
                            }


                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }

                }
                else
                {
                    if (propValue == null) // if object type is not intialized.
                    {
                        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);// Need to check
                    }
                }
                else
                {
                    GetProperties(property.PropertyType, ObjTypeName + "." + property.Name);

                }
            }

            return result;

        }

        private static bool CheckDuplicateKeyInDictionary(string keyName)
        {
            if (result.ContainsKey(keyName))
            {
                return true;
            }
            else
                return false;
        }
        
    }
Posted
Updated 5-Sep-18 4:32am
v2
Comments
OriginalGriff 5-Sep-18 10:33am    
And?
What do you need help with?
I see no sign of the method in that code - did you forget to include it?
Member 8025807 6-Sep-18 14:37pm    
Method is written inside the class. The method needs to return all kind of properties of a class object which it takes as argument. In complex scenario that means when properties are type of another class it is not working perfectly.
Member 8025807 6-Sep-18 14:37pm    
Method is written inside the class. The method needs to return all kind of properties of a class object which it takes as argument. In complex scenario that means when properties are type of another class it is not working perfectly.
[no name] 5-Sep-18 16:12pm    
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.

And?

because reflection does not work well for entitity framework and nestlings...

Who says? "Entities" are just "classes".

What's the point anyway? Serialize to XML if you want a "picture".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900