public FastCallableWithContext2(string name, CallTargetWithContext2 target)
 {
     this.target2 = target;
     this.name = name;
 }
Esempio n. 2
0
        /// <summary>
        /// Explicitly casts the object to a given type (and returns it as object)
        /// </summary>
        public static object Explicit(object o, Type to)
        {
            if (o == null)
            {
                // Null objects can be only cast to Nullable<T> or any reference type
                if (to.IsValueType)
                {
                    if (to.IsGenericType && to.GetGenericTypeDefinition() == NullableType)
                    {
                        return(NewNullableInstance(to.GetGenericArguments()[0]));
                    }
                    else
                    {
                        throw new InvalidCastException(String.Format("Cannot cast null to a value type {0}", to.Name));
                    }
                }
                else
                {
                    // Explicit cast to reference type is simply null
                    return(null);
                }
            }

            if (to.IsValueType)
            {
                return(ExplicitCastToValueType(o, to));
            }
            else
            {
                Type type = o.GetType();
                if (to.IsInstanceOfType(o) || to.IsAssignableFrom(type))
                {
                    return(o);
                }
                else
                {
                    // bad glue
                    if (type == typeof(CallTargetWithContextN))
                    {
                        CallTargetWithContextN ct = o as CallTargetWithContextN;
                        if (to == typeof(CallTargetWithContext0))
                        {
                            CallTargetWithContext0 t = delegate(CodeContext cc) { return(ct(cc)); };
                            return(t);
                        }
                        if (to == typeof(CallTargetWithContext1))
                        {
                            CallTargetWithContext1 t = delegate(CodeContext cc, object p0) { return(ct(cc, p0)); };
                            return(t);
                        }

                        if (to == typeof(CallTargetWithContext2))
                        {
                            CallTargetWithContext2 t = delegate(CodeContext cc, object p0, object p1) { return(ct(cc, p0, p1)); };
                            return(t);
                        }

                        if (to == typeof(CallTargetWithContext3))
                        {
                            CallTargetWithContext3 t = delegate(CodeContext cc, object p0, object p1, object p2) { return(ct(cc, p0, p1, p2)); };
                            return(t);
                        }

                        if (to == typeof(CallTargetWithContext4))
                        {
                            CallTargetWithContext4 t = delegate(CodeContext cc, object p0, object p1, object p2, object p3) { return(ct(cc, p0, p1, p2, p3)); };
                            return(t);
                        }

                        if (to == typeof(CallTargetWithContext5))
                        {
                            CallTargetWithContext5 t = delegate(CodeContext cc, object p0, object p1, object p2, object p3, object p4) { return(ct(cc, p0, p1, p2, p3, p4)); };
                            return(t);
                        }
                    }
                    throw new InvalidCastException(String.Format("Cannot cast {0} to {1}", type.Name, to.Name));
                }
            }
        }