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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

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

  • The Power of Market Disruption: How to Detect Fraud With Graph Data
  • How to Get Plain Text From Common Documents in Java
  • Platform Engineering: A Strategic Response to the Growing Complexity of Modern Software Architectures
  • What the CrowdStrike Crash Exposed About the Future of Software Testing
  1. DZone
  2. Coding
  3. Languages
  4. C# Reflection: Dealing With AmbiguousMatchException

C# Reflection: Dealing With AmbiguousMatchException

By 
Snippets Manager user avatar
Snippets Manager
·
May. 05, 06 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
18.6K Views

Join the DZone community and get the full member experience.

Join For Free
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

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: