понедельник, 4 ноября 2019 г.

How can I use interface as a C# generic type constraint?

https://stackoverflow.com/questions/1096568/how-can-i-use-interface-as-a-c-sharp-generic-type-constraint

Is there a way to get the following function declaration?
public bool Foo<T>() where T : interface;
ie. where T is an interface type (similar to where T : class, and struct).
Currently I've settled for:
public bool Foo<T>() where T : IBase;

GetInterface from type

https://www.geeksforgeeks.org/c-sharp-type-getinterface-method/

using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface("IFormattable");
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface(null);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}


using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface("iformattable", true);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface(null, true);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}