Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:37:42+00:00 2026-05-31T05:37:42+00:00

Possible Duplicate: Operator Overloading with C# Extension Methods How can i overload those operators,

  • 0

Possible Duplicate:
Operator Overloading with C# Extension Methods

How can i overload those operators, i feel misunderstood by the compiler.

I think the core problem is that i try to overload an operator as an extension to a class. The type class does not have those operators, so i feel pretty safe in doing so – but my compiler disagrees wildly.

    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;
        }

    }

To clarify the domain specifics of those comparisons: class A : B {}, class B {} and class C {} A is greater than A and greater than B but smaller than the rest. Because A.IsCastableTo(B) and A.IsCastableTo(A);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:37:43+00:00Added an answer on May 31, 2026 at 5:37 am

    Like the others have said, C# doesn’t support extension operators. If you need to do this then you can implement a custom type, something like:

    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; }
        }
    }
    

    Alternatively it would probably be easier to implement a custom DynamicObject (C# 4+) which can delegate the calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Possible Duplicate: Operator overloading I was wonder how can I over load the Conditional
Possible Duplicate: Operator overloading Hi guys can some one please suggest a good tutorial
Possible Duplicate: Operator Overloading in PHP Is there a way to overload the =
Possible Duplicate: Overloading += in c++ Do I need to overload the += operator
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: Operator overloading in C If I have a struct: typedef struct myStruct
Possible Duplicate: Operator overloading I didn't find any thing that could help me in
Possible Duplicate: Operator Overloading in C++ as int + obj I override the *

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.