public void TestCanCast() { Duck duck = new Duck(); IInterface proxy = DuckTyping.Cast <IInterface>(duck); Assert.IsTrue(DuckTyping.CanCast <IInterface, Duck>(), "CanCast should have returned true."); Assert.IsTrue(DuckTyping.CanCast <IInterface>(duck), "CanCast should have returned true."); Assert.IsTrue(DuckTyping.CanCast <IInterface>(typeof(Duck)), "CanCast should have returned true."); Assert.IsTrue(DuckTyping.CanCast(typeof(IInterface), duck), "CanCast should have returned true."); Assert.IsTrue(DuckTyping.CanCast(typeof(IInterface), typeof(Duck)), "CanCast should have returned true."); Assert.IsTrue(DuckTyping.CanCast <Duck, IInterface>(), "CanCast should have returned true."); Assert.IsFalse(DuckTyping.CanCast <IFormattable>(duck), "CanCast should have returned false."); Assert.IsTrue(DuckTyping.CanCast <GeneralDelegate, SpecializedDelegate>(), "CanCast should have returned true."); Assert.IsTrue(DuckTyping.CanCast <SpecializedDelegate, GeneralDelegate>(), "CanCast should have returned true."); Assert.IsFalse(DuckTyping.CanCast <GeneralDelegate, EventHandler>(), "CanCast should have returned false."); Assert.IsTrue(DuckTyping.CanCast <AttributeTargets, string>(), "CanCast should have returned true."); Assert.IsTrue(DuckTyping.CanCast <string, AttributeTargets>(), "CanCast should have returned true."); Assert.IsFalse(DuckTyping.CanCast <AttributeTargets, DateTime>(), "CanCast should have returned false."); }
public void TestInterfaceProxyMethodVariance() { Duck duck = new Duck(); IInterface proxy = DuckTyping.Cast <IInterface>(duck); string stringValue = "String value."; int intValue = 5; Assert.AreEqual(stringValue, proxy.ContravariantMethod(stringValue), "Contravariant method returned wrong value."); Assert.IsNull(proxy.ContravariantMethod(null), "Passing and returning a null value to and from a contravariant method failed."); try { proxy.ContravariantMethod(new object[0]); Assert.Fail("Passing a non-string object through a contravariant method that takes a string parameter should have thrown an InvalidCastException."); } catch (InvalidCastException) { } try { proxy.ContravariantValueMethod(7); Assert.Fail("Passing a non-DateTime object through a contravariant method that takes a DateTime parameter should have thrown an InvalidCastException."); } catch (InvalidCastException) { } try { proxy.ContravariantValueMethod(null); Assert.Fail("Passing null through a contravariant method that takes a value type parameter should have thrown a NullReferenceException."); } catch (NullReferenceException) { } Assert.AreEqual("Method", proxy.ContravariantEnumMethod("Method"), "Contravariant method returned wrong enum value."); try { proxy.ContravariantEnumMethod(null); Assert.Fail("Passing null through a contravariant method that taks an enum parameter should have thrown a NullReferenceException."); } catch (NullReferenceException) { } Assert.AreEqual(stringValue, proxy.CovariantMethod(stringValue), "Covariant method returned wrong value."); Assert.IsNull(null, proxy.CovariantMethod(null), "Passing and returning a null value to and from a covariant method failed."); Assert.AreEqual(intValue, proxy.CovariantValueMethod(intValue), "Covariant method returned wrong int value."); Assert.AreEqual(AttributeTargets.Class, proxy.CovariantEnumMethod(AttributeTargets.Class), "Covariant method returned wrong enum value."); // Note: This next line is primarily to check whether the system handles a recursive cast properly. If it doesn't, // a StackOverflowException will be thrown. Assert.IsTrue(proxy.Equals(proxy.VariantByRecursiveCastMethod(duck)), "Variant by recursive cast method returned wrong value."); Assert.AreEqual(duck, proxy.VariantByUncastMethod(proxy), "Variant by uncast method returned wrong value."); }
public void TestInterfaceProxyToString() { Duck duck = new Duck(); IInterface proxy = DuckTyping.Cast <IInterface>(duck); Assert.AreEqual(duck.ToString(), proxy.ToString(), "Interface proxy does not properly forward calls to ToString method."); }
/// <summary> /// Register an 'OnUpdate' method for use in the editor. /// This should be done in the OnAwake method of the module, and will ensure that all modules have the /// registered method called in order of declaration of the module in the part file. /// </summary> /// <param name="module">Module that is being registered.</param> /// <param name="action">Method to call</param> public static void RegisterOnUpdateEditor(this PartModule module, Action action) { if (!HighLogic.LoadedSceneIsEditor) { return; } Part part = module.part; IOnEditorUpdateUtility utility; foreach (MonoBehaviour c in part.GetComponents <MonoBehaviour>()) { if (c.GetType().FullName == typeof(OnEditorUpdateUtility).FullName) { utility = DuckTyping.Cast <IOnEditorUpdateUtility>(c); goto found; } } PartModule onEditorUpdate = (PartModule)part.gameObject.AddComponent(OnEditorUpdateUtility.LatestVersion); utility = DuckTyping.Cast <IOnEditorUpdateUtility>(onEditorUpdate); part.Modules.Add(onEditorUpdate); found: utility.AddOnUpdate(module, action); }
public void TestInterfaceCast() { Duck duck = new Duck(); IInterface proxy = DuckTyping.Cast <IInterface>(duck); Assert.IsNotNull(proxy, "DuckTyping.Cast<IInterface>(duck) return null."); Assert.IsTrue(proxy is IDuckProxy, "Cast did not return a proxy."); Assert.AreEqual(duck, ((IDuckProxy)proxy).UnwrapDuck(), "Cast returned a proxy that refers to the wrong duck object."); Assert.AreEqual(duck, DuckTyping.Cast <Duck>(proxy), "Reverse cast returned the wrong value."); }
public static IProgcomCPU CreateCPU() { LoadTypes(); if (mCPUType == null) { throw new MissingReferenceException("Could not find ProgCom.CPUem"); } object cpu = Activator.CreateInstance(mCPUType); return(DuckTyping.Cast <IProgcomCPU>(cpu)); }
void Main() { MyAdder myAdder = new MyAdder(); // Even though ICanAdd is not implemented by MyAdder, // we can duck cast it because it implements all the members: ICanAdd adder = DuckTyping.Cast <ICanAdd>(myAdder); // Now we can call adder as you would any ICanAdd object. // Transparently, this call is being forwarded to myAdder. int sum = adder.Add(2, 2); }
public static IProgcomAssembler GetCompatibleAssembler(this IProgcomCPU cpu) { LoadTypes(); if (mAssemblerType == null) { throw new MissingReferenceException("Could not find ProgCom.Assembler2"); } object assembler = mCPUType.InvokeMember("getCompatibleAssembler", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, DuckTyping.Uncast(cpu), null); return(DuckTyping.Cast <IProgcomAssembler>(assembler)); }
public void TestInterfaceProxyEquals() { Duck duck1 = new Duck(); Duck duck2 = new Duck(); IInterface proxy1a = DuckTyping.Cast <IInterface>(duck1); IInterface proxy1b = DuckTyping.Cast <IInterface>(duck1); IInterface proxy2 = DuckTyping.Cast <IInterface>(duck2); Assert.IsTrue(proxy1a.Equals(proxy1b), "Interface proxy does not properly forward calls to the Equals method."); Assert.IsTrue(!proxy1a.Equals(proxy2), "Interface proxy overrides the Equals method improperly."); Assert.AreEqual(proxy1a.GetHashCode(), duck1.GetHashCode(), "Interface proxy does not properly forward calls to the GetHashCode method."); }
public void TestInterfaceProxyEvent() { Duck duck = new Duck(); IInterface proxy = DuckTyping.Cast <IInterface>(duck); object sender = this; AddingNewEventArgs e = new AddingNewEventArgs(); EventHandler eventHandler = new EventHandler(this.EventHandlerMethod); AddingNewEventHandler addingNewEventHandler = new AddingNewEventHandler(this.AddingNewEventHandlerMethod); m_Sender = null; m_EventArgs = null; proxy.Event += eventHandler; duck.RaiseEvent(sender, e); Assert.AreEqual(sender, m_Sender, "Proxy class did not properly forward adding of an event handler."); m_Sender = null; m_EventArgs = null; proxy.Event -= eventHandler; duck.RaiseEvent(sender, e); Assert.IsNull(m_Sender, "Proxy class did not properly forward removing of an event handler."); m_Sender = null; m_EventArgs = null; proxy.CovariantEvent += addingNewEventHandler; duck.RaiseCovariantEvent(sender, e); Assert.AreEqual(sender, m_Sender, "Proxy class did not properly forward adding of an event handler to a covariant event."); m_Sender = null; m_EventArgs = null; proxy.CovariantEvent -= addingNewEventHandler; duck.RaiseCovariantEvent(sender, e); Assert.IsNull(m_Sender, "Proxy class did not properly forward removing of an event handler from a covariant event."); m_Sender = null; m_EventArgs = null; proxy.ContravariantEvent += eventHandler; duck.RaiseContravariantEvent(sender, e); Assert.AreEqual(sender, m_Sender, "Proxy class did not properly forward adding of an event handler to a contravariant event."); m_Sender = null; m_EventArgs = null; proxy.ContravariantEvent -= eventHandler; duck.RaiseContravariantEvent(sender, e); Assert.IsNull(m_Sender, "Proxy class did not properly forward removing of an event handler from a contravariant event."); }
public void TestInterfaceProxyProperty() { Duck duck = new Duck(); IInterface proxy = DuckTyping.Cast <IInterface>(duck); string stringValue = "String value."; int intValue = 5; proxy.Property = stringValue; Assert.AreEqual(stringValue, proxy.Property, "Property not implemented correctly."); proxy.ValueProperty = intValue; Assert.AreEqual(intValue, proxy.ValueProperty, "Value type property not implemented correctly."); proxy[4] = stringValue; Assert.AreEqual(stringValue, proxy[4], "Indexed property not implemented correctly."); }
public static IProgcomMonitor CreateMonitor( Int32[] arr, UInt16 ptr, UInt16 chars, UInt16 colPtr, UInt16 modePointer) { LoadTypes(); if (mMonitorType == null) { throw new MissingReferenceException("Could not find ProgCom.Monitor"); } object mon = Activator.CreateInstance(mMonitorType, arr, ptr, chars, colPtr, modePointer); mMonitorType.GetField("visible", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .SetValue(mon, true); return(DuckTyping.Cast <IProgcomMonitor>(mon)); }
public void TestDelegateCast() { SpecializedDelegate specializedDelegate = new SpecializedDelegate(this.SpecializedDelegateMethod); GeneralDelegate generalDelegate = DuckTyping.Cast <GeneralDelegate>(specializedDelegate); Assert.IsTrue(generalDelegate.Target is IDuckProxy, "Cast did not return a delegate to a proxy."); Assert.AreEqual(specializedDelegate, ((IDuckProxy)(generalDelegate.Target)).UnwrapDuck(), "Cast returned a delegate to a proxy that does not refer to the original delegate."); Assert.AreEqual(specializedDelegate, DuckTyping.Cast <SpecializedDelegate>(generalDelegate), "Reverse cast returned the wrong value."); bool b = (bool)(generalDelegate("true")); generalDelegate = new GeneralDelegate(this.GeneralDelegateMethod); specializedDelegate = DuckTyping.Cast <SpecializedDelegate>(generalDelegate); Assert.IsTrue(specializedDelegate.Target is IDuckProxy, "Cast did not return a delegate to a proxy."); Assert.AreEqual(generalDelegate, ((IDuckProxy)(specializedDelegate.Target)).UnwrapDuck(), "Cast returned a delegate to a proxy that does not refer to the original delegate."); Assert.AreEqual(generalDelegate, DuckTyping.Cast <GeneralDelegate>(specializedDelegate), "Reverse cast returned the wrong value."); specializedDelegate("true"); }
public void TestInterfaceProxyMethod() { IInterface duck = DuckTyping.Cast <IInterface>(new Duck()); duck.Method(); duck.Method(Duck.A, Duck.B, Duck.C, Duck.D); string stringValue = "String value."; Assert.AreEqual(stringValue, duck.Method(stringValue), "Method returned wrong string value."); Assert.IsNull(duck.Method(null), "Passing and returning a null value failed."); int intValue = 5; Assert.AreEqual(intValue, duck.Method(intValue), "Method returned wrong int value."); Assert.IsTrue(duck.BestMatchMethod(stringValue), "When generating the proxy class, the best matching method overload on the duck class was not chosen to forward calls to."); }
private static Part AsPart(object src) { Part part = src as Part; if (part != null) { return(part); } PartModule module = src as PartModule; if (module != null) { return(module.part); } if (src.GetType().GetInterfaces().Any(t => t.FullName == typeof(IPartMessagePartProxy).FullName || t.FullName == "KSPAPIExtensions.PartMessagePartProxy")) { return(DuckTyping.Cast <IPartMessagePartProxy>(src).ProxyPart); } return(null); }
internal static IPartMessageListenerV1 AsListener(object attr) { return(DuckTyping.Cast <IPartMessageListenerV1>(attr)); }
public void TestInterfaceProxyGenericMethod() { IInterface duck = DuckTyping.Cast <IInterface>(new Duck()); Assert.AreEqual("A", duck.GenericMethod <string>("A"), "Generic method returned wrong value."); }
internal static IPartMessageEventV1 AsEvent(object attr) { return(DuckTyping.Cast <IPartMessageEventV1>(attr)); }
public void TestGenericVarianceCast() { List <string> list = new List <string>(); IEnumerable <object> enumerable = DuckTyping.Cast <IEnumerable <object> >(list); }
internal static IPartMessageDelegateV1 AsDelegate(Attribute attribute) { return(DuckTyping.Cast <IPartMessageDelegateV1>(attribute)); }
public void TestEnumCast() { Assert.AreEqual(AttributeTargets.All, DuckTyping.Cast <AttributeTargets>("All"), "Cast from a string to an enumeration type returned the wrong value."); Assert.AreEqual("All", DuckTyping.Cast <string>(AttributeTargets.All), "Cast from an enumeration type to a string returned the wrong value."); }