DuckType target object instance is null
상속: DuckTypeException
    /// <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();
        }
    }
예제 #2
0
    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);
    }
예제 #3
0
    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);
    }
예제 #4
0
    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);
    }
예제 #5
0
    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);
    }
예제 #6
0
    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);
    }
예제 #7
0
    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);
    }