-2

I am trying to figure out how to check if I have a Dictionary or Single value like Guid in a List. Tried Type but only define the Array or Object, anybody has any suggestions?

 JArray P = R as JArray;
 try
 {
     JTokenType Type = P.Type;
     Result = P.ToObject<List<Dictionary<string, object>>>();
 }
 catch (Exception e)
 {

 }

Quick and dirty I thought about to catch the error, but not a nice sight.

Seems info was not correct, the objects are

 List<SomeClass>

or

 List<Guid> 

Example data:

 [
   {
      "Id": "45c41ef8-a030-4480-820d-5bd6bf02bf14",
      "UserId": "246fef1a-e85e-49be-9aad-7d32c8144580",
      "Type": 351208,
      "Name": "*******il.com",
      "isDirect": true,
      "Active": true,
      "History": []
   }
 ]

or

 ["b0d2dd7d-018c-43ad-9c70-8ec9a3f41978"]
user3763117
  • 327
  • 1
  • 5
  • 18
  • 1
    Welcome to Stack Overflow. I'm finding it very hard to understand your question. It would be much easier to see what you're trying to do if you could provide a [mcve]. – Jon Skeet Nov 08 '19 at 17:56
  • You mean given a List you want to find out what T is? [How to get the type of T from a member of a generic class or method?](https://stackoverflow.com/a/557349/243245) – Rup Nov 08 '19 at 18:00
  • Exception handling is a pet peeve of mine and this one is terrible. Trying to ignore a error like this is never going to work out. And you catch way to wide. Here are two articles on Exception Handling that I link often: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET | I would be nessesary to know the exception, to tell you proper handling – Christopher Nov 08 '19 at 18:06
  • I don't understand the question. What is your goal and your difficulty? Please try to better explain your issue. –  Nov 08 '19 at 18:31

2 Answers2

1

I'm not entirely sure, but I think you're wondering how to check if your List contains Dictionary or not.. But because Dictionary is a generic type, you have to know the specific types of the Dictionary contents if you want to use is

i.e. this is your problem:

//can't do
if(myList[0] is Dictionary)

//can only do this
if(myList[0] is Dictionary<string, object>)

Instead we have to do something like:

Type t = myList[0].GetType();
bool isDict = t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dictionary<,>);

If you also want to know the types in your dictionary, check the array returned by t.GetGenericArguments()

See this fiddle for a demo: https://dotnetfiddle.net/oyyivN

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I just tested it, both return false, for some strange reason – user3763117 Nov 08 '19 at 18:19
  • `myList[0] is Dictionary` will return false on a List but return true on a `List>` - but note that we are testing the type of the first entry in the list - you have to have at least one entry for it to work that way.. Otherwise if your list doesnt have anything inside (0 elements) you'll have to call `GetType().GetGenericArguments()[0]` on the List to get the Type of the items it holds, and then query THAT type for IsGeneric etc – Caius Jard Nov 08 '19 at 18:23
  • See https://dotnetfiddle.net/oyyivN – Caius Jard Nov 08 '19 at 18:31
  • 1
    Playing now with your advice, let you know what I come up with – user3763117 Nov 08 '19 at 18:37
  • The .GetGenericArguments()[0] gives the error for a strange reason ...I copied the List in queston – user3763117 Nov 08 '19 at 18:55
  • Can you reproduce it? I tried with a non-dictionary argument too, but couldn't https://dotnetfiddle.net/rwQKjy – Caius Jard Nov 08 '19 at 19:01
  • Error is {"Index was outside the bounds of the array."}, also the data is wrapped in a object – user3763117 Nov 08 '19 at 19:04
  • https://learn.microsoft.com/en-us/dotnet/api/system.type.getgenericarguments?view=netframework-4.8 says *Returns An array of Type objects that represent the type arguments of a generic type. **Returns an empty array if the current type is not a generic type.**..* - so the reason [0] is out of bounds is because the returned array is empty, because the thing you're querying isn't a List? – Caius Jard Nov 08 '19 at 19:07
  • No it not returns empty, I copied a sample set in the question, otherwise I would understand it – user3763117 Nov 08 '19 at 19:09
  • Not really sure what to suggest; investigate the object you have in the Locals/Immediate window of the debugger? – Caius Jard Nov 08 '19 at 20:15
  • The problem is that it does not have a key/value and only a value, so for now I will block the only value one, but thx for your help and idd I tested your solution it worked – user3763117 Nov 08 '19 at 20:39
  • Not quite sure how a `Dictionary` can have only a key and not a value - that sounds like a HashSet? But glad you got something going! :) – Caius Jard Nov 08 '19 at 21:16
  • @user3763117: That sounds like a Bag, Set, Queue, Stack or similar Key-& Indexless collection. In wich case you always wanted the value, not the key. – Christopher Nov 09 '19 at 10:23
0

If you want to check a single List<T>, it is just plain old Contains().

The second case is harder, for multiple reasons:

  1. Because you got a List of Dictionaties. So you have to itterate over the List Elements to even get to the thing that has the data or key you want.
  2. Because you got a List of Dictionaries. Wich means you have to decide if you want to use ContainsKey() or ContainsValue() checks.

It would help a lot if we had some actuall examples for those types. This is not enough to give you more then a general direction.

P.S.: Please never use object as type in a generic class. It ruins type saftey. As a mater of fact, Generics were introduced so we finally could stop using object. We still have those pre-generic collections floating around the Namespaces, like ghosts of the past.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • Yeah but its and internal handler, not really a fan also from dynamic and those fancey stuff). I am not sure if I solve the problem with on contains as the Dictionary contains variable keys which I have no idea off. – user3763117 Nov 08 '19 at 18:05
  • @user3763117 It is not clear what you even want to find in those Collections. It does not help that both have utterly different types. – Christopher Nov 08 '19 at 18:07