static void Main() { var expl = new ExplicitImpl(); expl.Bar(); // via extension method var impl = new ImplicitImpl(); impl.Bar(); // direct }
public void ExplicitImplementation() { { var c = new ExplicitImpl(); MethodInfo m0 = c.GetType().GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); Assert.That(m0, Is.Null, "Start is not found..."); Assert.That(typeof(IExplicit).FullName, Is.EqualTo("CK.Reflection.Tests.HelperTest+IExplicit")); MethodInfo m1 = c.GetType().GetMethod("CK.Reflection.Tests.HelperTest+IExplicit.Start".Replace('+', '.'), BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(m1, Is.Not.Null); Assert.That(m1.Invoke(c, null), Is.True); Assert.That(m1.DeclaringType, Is.EqualTo(c.GetType()), "To obtain an explicit implementation, one can use the FullName of the properties, ignoring nested class marker (+)."); Assert.That(c.GetType().GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Length > 0, "Default ctor is accessible."); } { var c = new ImplicitImpl(); MethodInfo m0 = c.GetType().GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); Assert.That(m0, Is.Not.Null, "Start exists."); MethodInfo m1 = c.GetType().GetMethod("CK.Reflection.Tests.HelperTest+IExplicit.Start".Replace('+', '.'), BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(m1, Is.Null, "But the explicit does not exist... Implicit hides explicit implementation."); } { ExplicitAndImplicitImpl c2 = new ExplicitAndImplicitImplSpecialized(); MethodInfo m0 = c2.GetType().GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); Assert.That(m0, Is.Not.Null, "Found the exposed one."); Assert.That(m0.Invoke(c2, null), Is.False); MethodInfo m1 = c2.GetType().GetMethod("CK.Reflection.Tests.HelperTest+IExplicit.Start".Replace('+', '.'), BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(m1, Is.Not.Null); Assert.That(m1.Invoke(c2, null), Is.True); Assert.That(m1.DeclaringType, Is.EqualTo(typeof(ExplicitAndImplicitImpl)), "Both exist, both are found."); Assert.That(c2.GetType().GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)[0].GetParameters().Length == 0, "The .ctor() is accessible."); Assert.That(typeof(ExplicitAndImplicitImpl).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)[0].GetParameters()[0].ParameterType, Is.EqualTo(typeof(int)), "The protected .ctor(int) is accessible."); } }
public void ExplicitImplementation() { { var c = new ExplicitImpl(); MethodInfo m0 = c.GetType().GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); Assert.That(m0, Is.Null, "Start is not found..."); MethodInfo m1 = c.GetType().GetMethod("CK.Reflection.Tests.HelperTest.IExplicit.Start", BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(m1, Is.Not.Null); Assert.That(m1.Invoke(c, null), Is.True); Assert.That(m1.DeclaringType, Is.EqualTo(c.GetType())); Assert.That(c.GetType().GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Length > 0, "Default ctor is accessible."); } { var c = new ImplicitImpl(); MethodInfo m0 = c.GetType().GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); Assert.That(m0, Is.Not.Null, "Start exists."); MethodInfo m1 = c.GetType().GetMethod("CK.Reflection.Tests.HelperTest.IExplicit.Start", BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(m1, Is.Null, "But the explicit does not exist..."); } { ExplicitImpl2 c2 = new ExplicitImpl3(); MethodInfo m0 = c2.GetType().GetMethod("Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); Assert.That(m0, Is.Not.Null, "Found the exposed one."); Assert.That(m0.Invoke(c2, null), Is.False); MethodInfo m1 = c2.GetType().GetMethod("CK.Reflection.Tests.HelperTest.IExplicit.Start", BindingFlags.Instance | BindingFlags.NonPublic); Assert.That(m1, Is.Not.Null); Assert.That(m1.Invoke(c2, null), Is.True); Assert.That(m1.DeclaringType, Is.EqualTo(typeof(ExplicitImpl2))); Assert.That(c2.GetType().GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)[0].GetParameters().Length == 0, "The .ctor() is accessible."); Assert.That(typeof(ExplicitImpl2).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)[0].GetParameters()[0].ParameterType, Is.EqualTo(typeof(int)), "The protected .ctor(int) is accessible."); } }
public void ExplicitImplementation() { { var c = new ExplicitImpl(); MethodInfo m0 = c.GetType().GetMethod( "Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ); Assert.That( m0, Is.Null, "Start is not found..." ); MethodInfo m1 = c.GetType().GetMethod( "CK.Reflection.Tests.HelperTest.IExplicit.Start", BindingFlags.Instance | BindingFlags.NonPublic ); Assert.That( m1, Is.Not.Null ); Assert.That( m1.Invoke( c, null ), Is.True ); Assert.That( m1.DeclaringType, Is.EqualTo( c.GetType() ) ); Assert.That( c.GetType().GetConstructors( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ).Length > 0, "Default ctor is accessible." ); } { var c = new ImplicitImpl(); MethodInfo m0 = c.GetType().GetMethod( "Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ); Assert.That( m0, Is.Not.Null, "Start exists." ); MethodInfo m1 = c.GetType().GetMethod( "CK.Reflection.Tests.HelperTest.IExplicit.Start", BindingFlags.Instance | BindingFlags.NonPublic ); Assert.That( m1, Is.Null, "But the explicit does not exist..." ); } { ExplicitImpl2 c2 = new ExplicitImpl3(); MethodInfo m0 = c2.GetType().GetMethod( "Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ); Assert.That( m0, Is.Not.Null, "Found the exposed one." ); Assert.That( m0.Invoke( c2, null ), Is.False ); MethodInfo m1 = c2.GetType().GetMethod( "CK.Reflection.Tests.HelperTest.IExplicit.Start", BindingFlags.Instance | BindingFlags.NonPublic ); Assert.That( m1, Is.Not.Null ); Assert.That( m1.Invoke( c2, null ), Is.True ); Assert.That( m1.DeclaringType, Is.EqualTo( typeof( ExplicitImpl2 ) ) ); Assert.That( c2.GetType().GetConstructors( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public )[0].GetParameters().Length == 0, "The .ctor() is accessible." ); Assert.That( typeof( ExplicitImpl2 ).GetConstructors( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public )[0].GetParameters()[0].ParameterType, Is.EqualTo( typeof( int ) ), "The protected .ctor(int) is accessible." ); } }
public void ExplicitImplementation() { { var c = new ExplicitImpl(); MethodInfo m0 = c.GetType().GetMethod( "Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ); Assert.That( m0, Is.Null, "Start is not found..." ); Assert.That( typeof(IExplicit).FullName, Is.EqualTo( "CK.Reflection.Tests.HelperTest+IExplicit" ) ); MethodInfo m1 = c.GetType().GetMethod( "CK.Reflection.Tests.HelperTest+IExplicit.Start".Replace( '+', '.' ), BindingFlags.Instance | BindingFlags.NonPublic ); Assert.That( m1, Is.Not.Null ); Assert.That( m1.Invoke( c, null ), Is.True ); Assert.That( m1.DeclaringType, Is.EqualTo( c.GetType() ), "To obtain an explicit implementation, one can use the FullName of the properties, ignoring nested class marker (+)." ); Assert.That( c.GetType().GetConstructors( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ).Length > 0, "Default ctor is accessible." ); } { var c = new ImplicitImpl(); MethodInfo m0 = c.GetType().GetMethod( "Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ); Assert.That( m0, Is.Not.Null, "Start exists." ); MethodInfo m1 = c.GetType().GetMethod( "CK.Reflection.Tests.HelperTest+IExplicit.Start".Replace( '+', '.' ), BindingFlags.Instance | BindingFlags.NonPublic ); Assert.That( m1, Is.Null, "But the explicit does not exist... Implicit hides explicit implementation." ); } { ExplicitAndImplicitImpl c2 = new ExplicitAndImplicitImplSpecialized(); MethodInfo m0 = c2.GetType().GetMethod( "Start", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ); Assert.That( m0, Is.Not.Null, "Found the exposed one." ); Assert.That( m0.Invoke( c2, null ), Is.False ); MethodInfo m1 = c2.GetType().GetMethod( "CK.Reflection.Tests.HelperTest+IExplicit.Start".Replace('+', '.'), BindingFlags.Instance | BindingFlags.NonPublic ); Assert.That( m1, Is.Not.Null ); Assert.That( m1.Invoke( c2, null ), Is.True ); Assert.That( m1.DeclaringType, Is.EqualTo( typeof( ExplicitAndImplicitImpl ) ), "Both exist, both are found." ); Assert.That( c2.GetType().GetConstructors( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public )[0].GetParameters().Length == 0, "The .ctor() is accessible." ); Assert.That( typeof( ExplicitAndImplicitImpl ).GetConstructors( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public )[0].GetParameters()[0].ParameterType, Is.EqualTo( typeof( int ) ), "The protected .ctor(int) is accessible." ); } }