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

  • Home
  • SEARCH
  • 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 3322706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:12:50+00:00 2026-05-17T23:12:50+00:00

This is best illustrated with an example: class Cat { } class Dog {

  • 0

This is best illustrated with an example:

class Cat
{
}

class Dog
{
    public static implicit operator Cat(Dog d)
    {
        return new Cat();
    }
}

I want to tell, for an arbitrary object, if I can cast it to Cat. Sadly I cannot seem to use the is/as operator.

void Main()
{
    var d = new Dog();
    if (d is Cat) throw new Exception("d is Cat");
    var c1 = (Cat)d; // yes
    //var c2 = d as Cat; // Won't compile: Cannot convert type 'Dog' to 'Cat' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

}

I’m hoping to avoid a try/catch(InvalidCastException) as I may be doing this a lot and this would be quite expensive.

Is there a way to do this cheaply and easily?

edit: Thanks for the answers guys – votes for all, wish I could give you all the Tick, but it’s going to Marc for the most general solution (bonus vote for punching it out on an ipod). However Jordao’s solution managed to hone in on what I need and not what I asked for so it is probably what I’m going with.

  • 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-17T23:12:51+00:00Added an answer on May 17, 2026 at 11:12 pm

    Conversion operators aren’t checked by is/as. You would needyo use reflection both to check for, and to invoke, said operators. I would write a generic static class

    static class Convert<TFrom,TTo> {}
    

    And in the static constructor check for the method, an use Delegate.CreateDelegate to create a

    Func<TFrom,TTo>
    

    And store that in a static field. Then you have fast access to the typed method:

    public static TTo Convert(TFrom obj) { return del(obj); }
    public static bool CanConvert { get { return del != null; } }
    private static readonly Func<TFrom,TTo> del;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably best shown with an example. I have an enum with attributes:
Is this the best way to handle file moving in a windows service? we
After reading this answer: best way to pick a random subset from a collection?
I need some ideas on how I can best solve this problem. I have
I'm trying to figure out how to best lay this out. I'll explain what
I'm having some conceptual trouble on figuring out how to best implement this... I
This question will expand on: Best way to open a socket in Python When
This question is about best use scenarios in projects with ORMs like NHibernate, Subsonic,
This question originally asked which is the best method for uploading files via SFTP
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE

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.