0

I have a method that is called like this

var data = new List<string>{"foo", value};
var event = MakeEvents("module", new object[]{data, "Notes:", notes, "Date:", DateTime.Now);

within MakeEvents, I break up the objectData array with a foreach look

foreach (var obj in objectData)
        {
            if (obj.ToString().Contains("List"))
                ev.data = SerialiseData((List<string>)obj);
            if (obj.GetType().ToString().Contains("System.DateTime"))
                ev.date = (DateTime)obj;
            if (obj.GetType().ToString().Contains("Boolean"))
                ev.is_deleted = (bool)obj;
            if (obj.GetType().ToString().Contains("Guid"))
                ev.parent_object_id = ((Guid)obj).ToString();
            if (obj.GetType().ToString() == "System.String")
            {
                var t = ((string)obj).Split(':').ToList();
                if (t.Count == 1)
                    ev.notes = (string)obj;
                else
                {
                    switch (t[0])
                    {
                        case "Notes":
                            ev.notes = t[1];
                            break;
                        case "Updated":
                            ev.updated_by = t[1];
                            break;
                        case "Parent":
                            ev.parent_object_id = t[1];
                            break;
                    }
                }
            }

The issue I'm having is that the first parameter in the object is List, but it is not being recognised as such, but does give me a count of the number of objects within data.

I've tried using Enumerable.Count(obj) to get the number objects within data, but I'm given an error that I'm mixing Enumerable with IEnumerable (The type arguments for method System.Linq.Enumerable.Count(this System.Collections.Generic.IEnumerable) cannot be inferred from the usage).

Is there a simple way of obtaining the count from obj?

3
  • Should the first if statement be if (obj.ToString().Contains("List")) or if (obj.GetType().ToString().Contains("List"))? Commented Jun 12, 2014 at 11:40
  • 1
    You should use if obj is bool instead of trying to match the name of the type. Commented Jun 12, 2014 at 11:40
  • 2
    Your type checking is really fragile. Use proper type checking Commented Jun 12, 2014 at 11:42

1 Answer 1

2

Your condition is trying to match the result of calling ToString on the list. You should use is instead:

if (obj is List<string>) { ... }

and likewise for all your other dynamic type checks.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - and thanks for all the other comments too. Much appreciated.
For this though, data is not being recognised as being List<string> but when viewed in the debugger I'm seeing objData[2], then in the loop obj[3] with obj[0] being the list - but not showing as a list, but an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.