code_junkie

How can I work around Type.IsAssignableFrom not working with inherited interfaces?

Nov 13th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. public interface IParentInterface
  2. {
  3.  
  4. }
  5.  
  6. public interface IChildInterface : IParentInterface
  7. {
  8.  
  9. }
  10.  
  11. public class ParentClass<T> where T: IParentInterface
  12. {
  13.  
  14. }
  15.  
  16. public class ChildClass : ParentClass<IChildInterface>
  17. {
  18.  
  19. }
  20.  
  21. public class TestClass
  22. {
  23. public ChildClass Property { get; set; }
  24. }
  25.  
  26. [TestFixture]
  27. public class ScratchPad
  28. {
  29.  
  30. [Test]
  31. public void Assignabl()
  32. {
  33. var tc = new TestClass();
  34. var tct = tc.GetType();
  35.  
  36. var pi = tct.GetProperty("Property");
  37.  
  38. Assert.IsNotNull(pi);
  39.  
  40. Assert.IsTrue(typeof(ParentClass<IChildInterface>).IsAssignableFrom(pi.PropertyType));
  41. Assert.IsTrue(typeof(ParentClass<>).IsAssignableFrom(pi.PropertyType));
  42. Assert.IsTrue(typeof(ParentClass<IParentInterface>).IsAssignableFrom(pi.PropertyType));
  43.  
  44. }
  45.  
  46. }
  47.  
  48. public class ParentClass<ParentInterface>
  49.  
  50. public class ChildClass : ParentClass<ChildInterface>
  51.  
  52. public class ParentClass<T>
  53.  
  54. interface IParent { }
  55. interface IChild : IParent { }
  56.  
  57. Parent<IChild>
  58.  
  59. Parent<IParent>
  60.  
  61. List<ParentInterface> parentList = List<ParentInterface>();
  62. List<ChildInterface> childList = parentList;
  63.  
  64. public interface ParentInterface2 : ChildInterface {}
  65.  
  66. childList.Add(new ParentInterface2Implementation());
Add Comment
Please, Sign In to add comment