Yushatak Yushatak - 1 year ago 55
C# Question

Type or Namespace 't' could not be found in lambda clearly defining t

public static bool Is(this object o, IEnumerable<Type> types)
return types.Any(t => o is t);

It's a simple extension method to test whether an object is one of the provided types - used to make code more readable.

Anyway it says it can't find what
is (the second instance of
). It's pretty clearly defined right before its use in the lambda function, and hovering over the first instance of it Intellisense even tells me that it's (parameter)
Type t

Pretty confused about this, since I don't know why VS can't figure it out. Occasionally Intellisense acts up and restarting VS will fix it, but not this time.

(BTW, this is VS2013)

Edit: I disagree with this being a duplicate. The question comes from a totally different angle and has a different accepted answer.

Answer Source

The problem is that the is operator requires an object on the left and a type name on the right. A literal type name, not an instance of class System.Type.

From your code, I guess that what you want to do is check if a certain object is an instance of some type represented by its System.Type. In this case, you need to use the IsInstanceOfType method of class Type

return types.Any(t => t.IsInstanceOfType(o));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download