Click here to Skip to main content
15,887,430 members
Home / Discussions / C#
   

C#

 
AnswerRe: error: "Not a legal OleAut"when importing Excel2007 into SQL Pin
Bernhard Hiller24-Sep-14 20:56
Bernhard Hiller24-Sep-14 20:56 
QuestionRegarding a basic question about Serialization Pin
Tridip Bhattacharjee23-Sep-14 21:26
professionalTridip Bhattacharjee23-Sep-14 21:26 
AnswerRe: Regarding a basic question about Serialization PinPopular
Eddy Vluggen23-Sep-14 21:50
professionalEddy Vluggen23-Sep-14 21:50 
AnswerRe: Regarding a basic question about Serialization Pin
George Jonsson24-Sep-14 18:14
professionalGeorge Jonsson24-Sep-14 18:14 
AnswerRe: Regarding a basic question about Serialization Pin
Sibeesh KV24-Sep-14 18:43
professionalSibeesh KV24-Sep-14 18:43 
Questionusing Peachtree API in C# Pin
Jassim Rahma23-Sep-14 11:08
Jassim Rahma23-Sep-14 11:08 
AnswerRe: using Peachtree API in C# Pin
jschell23-Sep-14 11:58
jschell23-Sep-14 11:58 
QuestionIterate over a Collection-Type for its Values using Reflection Pin
Agent__00723-Sep-14 1:48
professionalAgent__00723-Sep-14 1:48 
OK, this may be a stupid question, but I am trying to write an utility method to convert an instance of a class to Dictionary<string, string> where the key being the property's name and the value being its value. I have already taken care of string and the primitive types and other types. The problem is with System.Array and collections.

What I am trying to achieve is, if I have an instance of a class having ICollection<string> MyProperty property with a few string values, the dictionary item for this property should be something like {MyProperty, {Val1,Val2,..}} i.e. comma separated values as the value for the dictionary item.

The method I have is:
C#
private string GetValueForNonPrimitiveProperty(PropertyInfo property)
        {
            StringBuilder value = new StringBuilder();

            if (property.PropertyType == typeof(string))
            {
                value.Append(property.GetValue(this));
            }
            else if (property.PropertyType.Namespace.Contains(CollectionsNamespace) || property.PropertyType.IsArray)
            {
                var collection = property.GetValue(this);

                // iterate over the collection and form comma separated string for values
            }
            ... // other types here..

            return value.ToString();
        }
is giving me proper value-collection in collection variable along with "appropriate" type but only at runtime (it's an object at compile-time). But since I only get to know the "actual" type of the collection (like List<string>, List<int>, etc, but List of only simple types), I am not sure how to iterate through those values.

I tried casting collection to List<object> hoping to then iterate over after casting, but the cast is returning a null.

Also, simply checking the property-type's namespace against "System.Collections" doesn't seem to be the proper way to check for a collection-type. Please suggest.

Any help would be greatly appreciated.
Whether I think I can, or think I can't, I am always bloody right!


modified 23-Sep-14 7:58am.

AnswerRe: Iterate over a Collection-Type for its Values using Reflection Pin
BillWoodruff23-Sep-14 2:24
professionalBillWoodruff23-Sep-14 2:24 
GeneralRe: Iterate over a Collection-Type for its Values using Reflection Pin
Agent__00723-Sep-14 2:37
professionalAgent__00723-Sep-14 2:37 
GeneralRe: Iterate over a Collection-Type for its Values using Reflection Pin
BillWoodruff23-Sep-14 4:09
professionalBillWoodruff23-Sep-14 4:09 
GeneralRe: Iterate over a Collection-Type for its Values using Reflection Pin
Agent__00723-Sep-14 18:41
professionalAgent__00723-Sep-14 18:41 
GeneralRe: Iterate over a Collection-Type for its Values using Reflection Pin
BillWoodruff23-Sep-14 20:29
professionalBillWoodruff23-Sep-14 20:29 
SuggestionRe: Iterate over a Collection-Type for its Values using Reflection Pin
George Jonsson23-Sep-14 2:38
professionalGeorge Jonsson23-Sep-14 2:38 
GeneralRe: Iterate over a Collection-Type for its Values using Reflection Pin
Agent__00723-Sep-14 18:24
professionalAgent__00723-Sep-14 18:24 
AnswerRe: Iterate over a Collection-Type for its Values using Reflection Pin
Simon_Whale23-Sep-14 4:17
Simon_Whale23-Sep-14 4:17 
GeneralRe: Iterate over a Collection-Type for its Values using Reflection Pin
Agent__00723-Sep-14 18:33
professionalAgent__00723-Sep-14 18:33 
AnswerRe: Iterate over a Collection-Type for its Values using Reflection Pin
Eddy Vluggen23-Sep-14 7:17
professionalEddy Vluggen23-Sep-14 7:17 
GeneralRe: Iterate over a Collection-Type for its Values using Reflection Pin
Agent__00723-Sep-14 18:11
professionalAgent__00723-Sep-14 18:11 
QuestionTCP/IP : Socket programming Pin
Rajeeva B J23-Sep-14 1:10
Rajeeva B J23-Sep-14 1:10 
QuestionRe: TCP/IP : Socket programming Pin
George Jonsson23-Sep-14 1:36
professionalGeorge Jonsson23-Sep-14 1:36 
AnswerRe: TCP/IP : Socket programming Pin
Rajeeva B J23-Sep-14 2:11
Rajeeva B J23-Sep-14 2:11 
SuggestionRe: TCP/IP : Socket programming Pin
George Jonsson23-Sep-14 2:46
professionalGeorge Jonsson23-Sep-14 2:46 
GeneralRe: TCP/IP : Socket programming Pin
Bernhard Hiller23-Sep-14 20:48
Bernhard Hiller23-Sep-14 20:48 
AnswerRe: TCP/IP : Socket programming Pin
Richard MacCutchan23-Sep-14 1:54
mveRichard MacCutchan23-Sep-14 1:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.