Getting error while converting a cllection of object prperties to dictionary RRS feed

  • Question

  • User213108198 posted

    My Code 

    Assuming that data is some object and that you want to put its public properties into a Dictionary then try

    Dictionary<string,string> FD = (from x in data.GetType().GetProperties() select x).ToDictionary ( x => x.Name, x => (x.GetGetMethod().Invoke (data, null) == null ? "" : x.GetGetMethod().Invoke (data, null).ToString()));

    Its throwing error missmatch parameter.. please help..
    Saturday, January 18, 2014 4:59 AM

All replies

  • User-760709272 posted

    The code compiles and works for me against a basic object.  The problem will be with one of the parameters of the actual object you are using as data.  It might be a problem with its type, or something else.  You might want to break the code down into sections rather than one line, it will help with debugging and will let you deal better with types or conditions you want to ignore.

    Saturday, January 18, 2014 8:34 AM