扩展“<”和“>” System.Type的运算符

时间:2012-03-12 13:01:39

标签: c# operator-overloading

  

可能重复:
  Operator Overloading with C# Extension Methods

如何重载这些运算符,我觉得编译器误解了。

我认为核心问题是我试图将运算符重载为类的扩展。类型类没有那些运算符,所以我觉得这样做非常安全 - 但我的编译器不同意。

    public static class TypeCheck
    {
        public static Boolean ToBool(this Type t1, Type t2)
        {
            //normal extension works
            return true;
        }

        public static Boolean operator > (this Type t1, Type t2)
        {
            //TODO once it compiles
            return fasle;
        }

        public static Boolean operator < (this Type t1, Type t2)
        {
            //TODO once it compiles
            return true;
        }

    }

要澄清这些比较的域名细节:class A : B {}class B {}class C {} A大于A且大于B但小于其余的。因为A.IsCastableTo(B)和A.IsCastableTo(A);

4 个答案:

答案 0 :(得分:7)

C#中没有“扩展操作符”。

至少有一个操作符参数必须是声明操作符的类型。

答案 1 :(得分:1)

您可以通过扩展方法方法重载您自己的类中的运算符,但不能重载其他类中的运算符。

答案 2 :(得分:1)

您无法将操作员行为定义为扩展方法 您可以使用与DateTime与Add()Subtract()一起使用的相同方式执行此操作,如果您无法修改类型实现,则可以将其实现为扩展方法。

您也可以从类型继承(如果可能),并在那里定义运算符。

答案 3 :(得分:1)

与其他人所说的一样,C#不支持扩展运营商。如果您需要这样做,那么您可以实现自定义类型,如:

public class MyType : Type
{
    private Type internalType;

    public MyType(Type t)
    {
        internalType = t;
    }

    public static Boolean operator >(MyType t1, Type t2)
    {
        //TODO once it compiles
        return false;
    }

    public static Boolean operator <(MyType t1, Type t2)
    {
        //TODO once it compiles
        return true;
    }

    public override Assembly Assembly
    {
        get { return internalType.Assembly; }
    }

    public override string AssemblyQualifiedName
    {
        get { return internalType.AssemblyQualifiedName; }
    }

    public override Type BaseType
    {
        get { return internalType.BaseType; }
    }

    public override string FullName
    {
        get { return internalType.FullName; }
    }

    public override Guid GUID
    {
        get { return internalType.GUID; }
    }

    protected override TypeAttributes GetAttributeFlagsImpl()
    {
        return internalType.Attributes;
    }

    protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
    {
        return internalType.GetConstructor(bindingAttr, binder, callConvention, types, modifiers);
    }

    public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
    {
        return internalType.GetConstructors(bindingAttr);
    }

    public override Type GetElementType()
    {
        return GetElementType();
    }

    public override EventInfo GetEvent(string name, BindingFlags bindingAttr)
    {
        return internalType.GetEvent(name, bindingAttr);
    }

    public override EventInfo[] GetEvents(BindingFlags bindingAttr)
    {
        return internalType.GetEvents(bindingAttr);
    }

    public override FieldInfo GetField(string name, BindingFlags bindingAttr)
    {
        return internalType.GetField(name, bindingAttr);
    }

    public override FieldInfo[] GetFields(BindingFlags bindingAttr)
    {
        return internalType.GetFields(bindingAttr);
    }

    public override Type GetInterface(string name, bool ignoreCase)
    {
        return internalType.GetInterface(name, ignoreCase);
    }

    public override Type[] GetInterfaces()
    {
        return internalType.GetInterfaces();
    }

    public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
    {
        return internalType.GetMembers(bindingAttr);
    }

    protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
    {
        return internalType.GetMethod(name, bindingAttr, binder, callConvention, types, modifiers);
    }

    public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
    {
        return internalType.GetMethods(bindingAttr);
    }

    public override Type GetNestedType(string name, BindingFlags bindingAttr)
    {
        return internalType.GetNestedType(name, bindingAttr);
    }

    public override Type[] GetNestedTypes(BindingFlags bindingAttr)
    {
        return internalType.GetNestedTypes(bindingAttr);
    }

    public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
    {
        return internalType.GetProperties(bindingAttr);
    }

    protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
    {
        return internalType.GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
    }

    protected override bool HasElementTypeImpl()
    {
        return internalType.HasElementType;
    }

    public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] namedParameters)
    {
        return internalType.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
    }

    protected override bool IsArrayImpl()
    {
        return internalType.IsArray;
    }

    protected override bool IsByRefImpl()
    {
        return internalType.IsByRef;
    }

    protected override bool IsCOMObjectImpl()
    {
        return internalType.IsCOMObject;
    }

    protected override bool IsPointerImpl()
    {
        return internalType.IsPointer;
    }

    protected override bool IsPrimitiveImpl()
    {
        return internalType.IsPrimitive;
    }

    public override Module Module
    {
        get { return internalType.Module; }
    }

    public override string Namespace
    {
        get { return internalType.Namespace; }
    }

    public override Type UnderlyingSystemType
    {
        get { return internalType.UnderlyingSystemType; }
    }

    public override object[] GetCustomAttributes(Type attributeType, bool inherit)
    {
        return internalType.GetCustomAttributes(attributeType, inherit);
    }

    public override object[] GetCustomAttributes(bool inherit)
    {
        return internalType.GetCustomAttributes(inherit);
    }

    public override bool IsDefined(Type attributeType, bool inherit)
    {
        return internalType.IsDefined(attributeType, inherit);
    }

    public override string Name
    {
        get { return internalType.Name; }
    }
}

或者,实现可以委派调用的自定义DynamicObject(C#4+)可能更容易。