Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object ReturnMatch(System.Type type)
- {
- foreach(object obj in myObjects)
- {
- if (obj == type)
- {
- return obj;
- }
- }
- }
- if (obj is type) // won't compile in C# 2.0
- if (obj.GetType().Equals(type) || obj.GetType().IsSubclassOf(type))
- Type theTypeWeWant; // From argument or whatever
- foreach (object o in myCollection)
- {
- if (theTypeWeWant.IsAssignableFrom(o.GetType))
- return o;
- }
- private T FindObjectOfType<T>() where T: class
- {
- foreach(object o in myCollection)
- {
- if (o is T)
- return (T) o;
- }
- return null;
- }
- if(obj is type)
- foreach(object obj in myObjects)
- {
- if (obj is type)
- {
- return obj;
- }
- }
- class Level1
- {
- }
- class Level2A : Level1
- {
- }
- class Level2B : Level1
- {
- }
- class Level3A2A : Level2A
- {
- }
- class Program
- {
- static void Main(string[] args)
- {
- object[] objects = new object[] {"testing", new Level1(), new Level2A(), new Level2B(), new Level3A2A(), new object() };
- ReturnMatch(typeof(Level1), objects);
- Console.ReadLine();
- }
- static void ReturnMatch(Type arbitraryType, object[] objects)
- {
- foreach (object obj in objects)
- {
- Type objType = obj.GetType();
- Console.Write(arbitraryType.ToString() + " is ");
- if (!arbitraryType.IsAssignableFrom(objType))
- Console.Write("not ");
- Console.WriteLine("assignable from " + objType.ToString());
- }
- }
- }
Add Comment
Please, Sign In to add comment