/// <summary> /// Checks and ensures the arguments for the Create methods /// </summary> /// <param name="proxyType">Duck type</param> /// <param name="instance">Instance value</param> /// <exception cref="ArgumentNullException">If the duck type or the instance value is null</exception> private static void EnsureArguments(Type proxyType, object instance) { if (proxyType is null) { DuckTypeProxyTypeDefinitionIsNull.Throw(); } if (instance is null) { DuckTypeTargetObjectInstanceIsNull.Throw(); } }
public static bool DuckIs(this object instance, Type targetType) { if (instance is null) { DuckTypeTargetObjectInstanceIsNull.Throw(); } if (targetType != null && (targetType.IsPublic || targetType.IsNestedPublic)) { return(DuckType.CanCreate(targetType, instance)); } return(false); }
public static bool DuckIs <T>(this object instance) { if (instance is null) { DuckTypeTargetObjectInstanceIsNull.Throw(); } if (DuckType.CreateCache <T> .IsVisible) { return(DuckType.CanCreate <T>(instance)); } return(false); }
public static object DuckAs(this object instance, Type targetType) { if (instance is null) { DuckTypeTargetObjectInstanceIsNull.Throw(); } if (targetType != null && (targetType.IsPublic || targetType.IsNestedPublic)) { DuckType.CreateTypeResult proxyResult = DuckType.GetOrCreateProxyType(targetType, instance.GetType()); if (proxyResult.Success) { return(proxyResult.CreateInstance(instance)); } } return(null); }
public static bool TryDuckCast(this object instance, Type targetType, out object value) { if (instance is null) { DuckTypeTargetObjectInstanceIsNull.Throw(); } if (targetType != null && (targetType.IsPublic || targetType.IsNestedPublic)) { DuckType.CreateTypeResult proxyResult = DuckType.GetOrCreateProxyType(targetType, instance.GetType()); if (proxyResult.Success) { value = proxyResult.CreateInstance(instance); return(true); } } value = default; return(false); }
public static T DuckAs <T>(this object instance) where T : class { if (instance is null) { DuckTypeTargetObjectInstanceIsNull.Throw(); } if (DuckType.CreateCache <T> .IsVisible) { DuckType.CreateTypeResult proxyResult = DuckType.CreateCache <T> .GetProxy(instance.GetType()); if (proxyResult.Success) { return(proxyResult.CreateInstance <T>(instance)); } } return(null); }
public static bool TryDuckCast <T>(this object instance, out T value) { if (instance is null) { DuckTypeTargetObjectInstanceIsNull.Throw(); } if (DuckType.CreateCache <T> .IsVisible) { DuckType.CreateTypeResult proxyResult = DuckType.CreateCache <T> .GetProxy(instance.GetType()); if (proxyResult.Success) { value = proxyResult.CreateInstance <T>(instance); return(true); } } value = default; return(false); }