using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using Developpez.Dotnet; using System.Collections; using Developpez.Dotnet.Reflection; 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()); } } }