using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Developpez.Dotnet.Reflection; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Reflection { [TestFixture] public class ReflectionExtensionsTests { [Test] public void Test_Is() { Assert.IsTrue(typeof(string).Is()); Assert.IsFalse(typeof(object).Is()); Assert.IsTrue(typeof(int).Is()); Assert.IsFalse(typeof(int).Is()); Assert.IsTrue(typeof(string).Is>()); Assert.IsTrue(typeof(string).Is()); Assert.IsTrue(typeof(int).Is()); Assert.IsTrue(typeof(IEnumerable).Is()); Assert.IsFalse(typeof(IEnumerable).Is>()); Assert.IsTrue(typeof(EventHandler).Is()); Assert.IsTrue(typeof(EventHandler).Is()); } #region Classes de test pour les attributs [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] class XAttribute : Attribute { } [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)] class YAttribute : Attribute { } [X, Y] class A { } class B : A { } #endregion [Test] public void Test_HasAttribute() { Assert.IsTrue(typeof(A).HasAttribute()); Assert.IsTrue(typeof(A).HasAttribute(typeof(XAttribute))); Assert.IsFalse(typeof(B).HasAttribute()); Assert.IsFalse(typeof(B).HasAttribute(typeof(XAttribute))); Assert.IsTrue(typeof(B).HasAttribute(true)); Assert.IsTrue(typeof(B).HasAttribute(typeof(YAttribute), true)); Assert.IsFalse(typeof(B).HasAttribute(false)); Assert.IsFalse(typeof(B).HasAttribute(typeof(YAttribute), false)); } [Test] public void Test_GetAttribute() { XAttribute ax1 = typeof(A).GetAttribute(); Attribute ax2 = typeof(A).GetAttribute(typeof(XAttribute)); Assert.IsNotNull(ax1); Assert.IsNotNull(ax2); XAttribute bx1 = typeof(B).GetAttribute(); Attribute bx2 = typeof(B).GetAttribute(typeof(XAttribute)); Assert.IsNull(bx1); Assert.IsNull(bx2); YAttribute by1 = typeof(B).GetAttribute(true); Attribute by2 = typeof(B).GetAttribute(typeof(YAttribute), true); YAttribute by3 = typeof(B).GetAttribute(false); Attribute by4 = typeof(B).GetAttribute(typeof(YAttribute), false); Assert.IsNotNull(by1); Assert.IsNotNull(by2); Assert.IsNull(by3); Assert.IsNull(by4); } [Test] public void Test_GetAttributes() { IEnumerable a1 = typeof(A).GetAttributes(); IEnumerable b1 = typeof(B).GetAttributes(true); IEnumerable b2 = typeof(B).GetAttributes(false); Assert.AreEqual(2, a1.Count()); Assert.AreEqual(1, b1.Count()); Assert.AreEqual(0, b2.Count()); IEnumerable ax1 = typeof(A).GetAttributes(); IEnumerable ax2 = typeof(A).GetAttributes(typeof(XAttribute)); IEnumerable bx1 = typeof(B).GetAttributes(); IEnumerable bx2 = typeof(B).GetAttributes(typeof(XAttribute)); Assert.AreEqual(1, ax1.Count()); Assert.AreEqual(1, ax2.Count()); Assert.AreEqual(0, bx1.Count()); Assert.AreEqual(0, bx2.Count()); IEnumerable by1 = typeof(B).GetAttributes(); IEnumerable by2 = typeof(B).GetAttributes(typeof(YAttribute)); IEnumerable by3 = typeof(B).GetAttributes(false); IEnumerable by4 = typeof(B).GetAttributes(typeof(YAttribute), false); Assert.AreEqual(1, by1.Count()); Assert.AreEqual(1, by2.Count()); Assert.AreEqual(0, by3.Count()); Assert.AreEqual(0, by4.Count()); } [Test] public void Test_IsNullable() { Assert.IsTrue(typeof(string).IsNullable()); Assert.IsFalse(typeof(int).IsNullable()); Assert.IsTrue(typeof(int?).IsNullable()); } [Test] public void Test_IsStatic() { Assert.IsTrue(typeof(ReflectionExtensions).IsStatic()); Assert.IsFalse(typeof(ReflectionExtensionsTests).IsStatic()); } #region Classes de test pour les conversions class Foo { private readonly int _x; public Foo(int x) { _x = x; } public int X { get { return _x; } } } class Bar { private readonly int _x; public Bar(int x) { _x = x; } public int X { get { return _x; } } public static explicit operator Foo(Bar b) { return new Foo(b.X); } } class Baz { private readonly int _x; public Baz(int x) { _x = x; } public int X { get { return _x; } } public static implicit operator Foo(Baz b) { return new Foo(b.X); } } #endregion [Test] public void Test_IsImplicitlyConvertibleFrom() { Assert.IsTrue(typeof(object).IsImplicitlyConvertibleFrom(typeof(Foo))); Assert.IsTrue(typeof(object).IsImplicitlyConvertibleFrom()); Assert.IsTrue(typeof (Foo).IsImplicitlyConvertibleFrom(typeof (Baz))); Assert.IsTrue(typeof(Foo).IsImplicitlyConvertibleFrom()); Assert.IsFalse(typeof(Foo).IsImplicitlyConvertibleFrom(typeof(Bar))); Assert.IsFalse(typeof(Foo).IsImplicitlyConvertibleFrom()); Assert.IsFalse(typeof(Foo).IsImplicitlyConvertibleFrom(typeof(string))); Assert.IsFalse(typeof(Foo).IsImplicitlyConvertibleFrom()); } [Test] public void Test_IsConvertibleFrom() { Assert.IsTrue(typeof(object).IsConvertibleFrom(typeof(Foo))); Assert.IsTrue(typeof(object).IsConvertibleFrom()); Assert.IsTrue(typeof(Foo).IsConvertibleFrom(typeof(Baz))); Assert.IsTrue(typeof(Foo).IsConvertibleFrom()); Assert.IsTrue(typeof(Foo).IsConvertibleFrom(typeof(Bar))); Assert.IsTrue(typeof(Foo).IsConvertibleFrom()); Assert.IsFalse(typeof(Foo).IsConvertibleFrom(typeof(string))); Assert.IsFalse(typeof(Foo).IsConvertibleFrom()); } [Test] public void Test_IsImplicitlyConvertibleTo() { Assert.IsTrue(typeof(Foo).IsImplicitlyConvertibleTo(typeof(object))); Assert.IsTrue(typeof(Foo).IsImplicitlyConvertibleTo()); Assert.IsTrue(typeof(Baz).IsImplicitlyConvertibleTo(typeof(Foo))); Assert.IsTrue(typeof(Baz).IsImplicitlyConvertibleTo()); Assert.IsFalse(typeof(Bar).IsImplicitlyConvertibleTo(typeof(Foo))); Assert.IsFalse(typeof(Bar).IsImplicitlyConvertibleTo()); Assert.IsFalse(typeof(string).IsImplicitlyConvertibleTo(typeof(Foo))); Assert.IsFalse(typeof(string).IsImplicitlyConvertibleTo()); } [Test] public void Test_IsConvertibleTo() { Assert.IsTrue(typeof(Foo).IsConvertibleTo(typeof(object))); Assert.IsTrue(typeof(Foo).IsConvertibleTo()); Assert.IsTrue(typeof(Baz).IsConvertibleTo(typeof(Foo))); Assert.IsTrue(typeof(Baz).IsConvertibleTo()); Assert.IsTrue(typeof(Bar).IsConvertibleTo(typeof(Foo))); Assert.IsTrue(typeof(Bar).IsConvertibleTo()); Assert.IsFalse(typeof(string).IsConvertibleTo(typeof(Foo))); Assert.IsFalse(typeof(string).IsConvertibleTo()); } #region Classes de test pour SetValue class MyClass { public int Foo { get; set; } #pragma warning disable 649 public int Bar; #pragma warning restore 649 } struct MyStruct { public int Foo { get; set; } #pragma warning disable 649 public int Bar; #pragma warning restore 649 } #endregion [Test] public void Test_SetValue_Property_WithClass() { var obj = new MyClass(); typeof(MyClass).GetProperty("Foo").SetValue(ref obj, 42); Assert.AreEqual(42, obj.Foo); } [Test] public void Test_SetValue_Property_WithStruct() { var obj = new MyStruct(); typeof(MyStruct).GetProperty("Foo").SetValue(ref obj, 42); Assert.AreEqual(42, obj.Foo); } [Test] public void Test_SetValue_Field_WithClass() { var obj = new MyClass(); typeof(MyClass).GetField("Bar").SetValue(ref obj, 42); Assert.AreEqual(42, obj.Bar); } [Test] public void Test_SetValue_Field_WithStruct() { var obj = new MyStruct(); typeof(MyStruct).GetField("Bar").SetValue(ref obj, 42); Assert.AreEqual(42, obj.Bar); } } }