DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Zero to AI Hero, Part 2: Understanding Plugins in Semantic Kernel, A Deep Dive With Examples
  • From Zero to AI Hero, Part 1: Jumpstart Your Journey With Semantic Kernel
  • Top 10 C# Keywords and Features

Trending

  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Production-Grade LLM Regression Testing: Contracts, Evals, and Quality Gates

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. C# Reflection: Dealing With AmbiguousMatchException

C# Reflection: Dealing With AmbiguousMatchException

By 
Snippets Manager
·
May. 05, 06 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
19.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in
C#
 
using System;
using System.Reflection;

class Myambiguous 
{
    // The first overload is typed to an Int32
    public static void Mymethod(Int32 number)
    {
        Console.Write("\n{0}", "I am from Int32 method");
    }

    // The second overload is typed to a string
    public static void Mymethod(string alpha)
    {
        Console.Write("\n{0}", "I am from a string.");
    }

    public static void Main()
    {
        try
        {
            // The following does not cause an exception
            Mymethod(2);  // goes to Mymethod (Int32)
            Mymethod("3");   // goes to Mymethod (string)

            Type Mytype = Type.GetType("Myambiguous");

            MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[] { typeof(Int32) });
            MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[] { typeof(System.String) });

            // Invoke a method, utilizing an Int32 integer
            Mymethodinfo32.Invoke(null, new Object[] { 2 });

            // Invoke the method utilizing a string
            Mymethodinfostr.Invoke(null, new Object[] { "1" });

            // The following line causes an ambiguous exception
            MethodInfo Mymethodinfo = Mytype.GetMethod("Mymethod");
        }  // end of try block
        catch (System.Reflection.AmbiguousMatchException theException)
        {
            Console.Write("\nAmbiguousMatchException message - {0}", theException.Message);
        }
        catch
        {
            Console.Write("\nError thrown");
        }
        return;
    }
}

// This code produces the following output:
// I am from Int32 method
// I am from a string.
// I am from Int32 method
// I am from a string.
// AmbiguousMatchException message - Ambiguous match found.


C# (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Zero to AI Hero, Part 2: Understanding Plugins in Semantic Kernel, A Deep Dive With Examples
  • From Zero to AI Hero, Part 1: Jumpstart Your Journey With Semantic Kernel
  • Top 10 C# Keywords and Features

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook