示例#1
0
        public static bool DuckIs <T>(this object instance)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            return(DuckType.CanCreate <T>(instance));
        }
示例#2
0
        public static bool DuckIs(this object instance, Type targetType)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (targetType != null)
            {
                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);
        }
示例#4
0
        public static object DuckAs(this object instance, Type targetType)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (targetType != null)
            {
                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)
            {
                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 bool TryDuckImplement(this object?instance, Type?typeToDeriveFrom, [NotNullWhen(true)] out object?value)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (typeToDeriveFrom != null)
            {
                DuckType.CreateTypeResult proxyResult = DuckType.GetOrCreateReverseProxyType(typeToDeriveFrom, instance.GetType());
                if (proxyResult.Success)
                {
                    value = proxyResult.CreateInstance(instance);
                    return(true);
                }
            }

            value = default;
            return(false);
        }
 /// <summary>
 /// Gets the duck type instance for the object implementing a base class or interface T
 /// </summary>
 /// <param name="instance">Object instance</param>
 /// <param name="targetType">Target type</param>
 /// <returns>DuckType instance</returns>
 public static object As(this object instance, Type targetType)
 => DuckType.Create(targetType, instance);
 /// <summary>
 /// Gets the duck type instance for the object implementing a base class or interface T
 /// </summary>
 /// <param name="instance">Object instance</param>
 /// <typeparam name="T">Target type</typeparam>
 /// <returns>DuckType instance</returns>
 public static T As <T>(this object instance)
 => DuckType.Create <T>(instance);
示例#9
0
 public static object DuckImplement(this object instance, Type typeToDeriveFrom)
 => DuckType.CreateReverse(typeToDeriveFrom, instance);
示例#10
0
 public static T?DuckCast <T>(this object?instance)
 => DuckType.Create <T>(instance);