Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列出类的通用接口名称_C#_Interface - Fatal编程技术网

C# 列出类的通用接口名称

C# 列出类的通用接口名称,c#,interface,C#,Interface,我有这个c代码 那么我有这个, public List<TrendingItem<ITrendingItem>> trendItems { get; set; } 及 string g=typeof(TrendingLocation).GetInterfaces()[0].Name; 但这两个都没有列出通用接口,如ITrendingCafe、iTrendingTaurant等 有没有办法获取通用接口名的名称?您想使用类型的方法 如果我理解你的结构,它将是这样的: Ty

我有这个c代码

那么我有这个,

public List<TrendingItem<ITrendingItem>> trendItems { get; set; }

string g=typeof(TrendingLocation).GetInterfaces()[0].Name;
但这两个都没有列出通用接口,如ITrendingCafe、iTrendingTaurant等


有没有办法获取通用接口名的名称?

您想使用类型的方法

如果我理解你的结构,它将是这样的:

Type[] typeArguments = fvm.trendItems[4].trendItem.GetType().GetGenericArguments();

foreach (Type tParam in typeArguments)
{
    // Compare the type with the interface you are looking for.
}

我认为
ITrendingCafe
是一个实现
ITrendingItem
的接口。我编写了一个快速程序,它获取并显示
T
实现的所有接口:

using System;
using System.Collections.Generic;

namespace TestConsoleApplication
{
    public interface ITrendingItem
    {
        string ItemName { get; set; }
    }

    public interface ITrendingCafe : ITrendingItem
    {
        string CafeName { get; set; }
    }


    public class TrendingItem<T> where T : ITrendingItem
    {
        public T trendItem { get; set; }
    }

    public class Cafe : ITrendingCafe
    {
        public string ItemName { get; set; }
        public string CafeName { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {
            var test = new List<TrendingItem<ITrendingItem>> { new TrendingItem<ITrendingItem> { trendItem = new Cafe() } };

            foreach (var trendingItem in test[0].trendItem.GetType().GetInterfaces())
            {
                Console.Out.WriteLine(trendingItem.Name);
            }
            Console.ReadKey();
        }
    }
}
使用系统;
使用System.Collections.Generic;
命名空间TestConsoleApplication
{
公共接口ITrendingItem
{
字符串ItemName{get;set;}
}
公共界面ITrendingCafe:ITrendingItem
{
字符串CafeName{get;set;}
}
公共类趋势项,其中T:ITrendingItem
{
公共T趋势项{get;set;}
}
公共类咖啡馆:ITrendingCafe
{
公共字符串ItemName{get;set;}
公共字符串CafeName{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
var test=new List{new TrendingItem{trendItem=new Cafe()};
foreach(测试[0]中的变量trendingItem]。trendItem.GetType().GetInterfaces())
{
Console.Out.WriteLine(trendingItem.Name);
}
Console.ReadKey();
}
}
}
以下是输出:


如您所见,接口就在那里。只需通过循环找到你需要的

您是在寻找专门化的泛型接口,还是在寻找用于专门化每个泛型接口的类型?我想要一个列表,这样我就可以将它的名称放在网页上的类标记中,然后网页将有一个相应的css类用于样式设置
string g = fvm.trendItems[4].trendItem.GetType().GetInterfaces()[1].Name;
string g = typeof(TrendingLocation<>).GetInterfaces()[0].Name;
Type[] typeArguments = fvm.trendItems[4].trendItem.GetType().GetGenericArguments();

foreach (Type tParam in typeArguments)
{
    // Compare the type with the interface you are looking for.
}
using System;
using System.Collections.Generic;

namespace TestConsoleApplication
{
    public interface ITrendingItem
    {
        string ItemName { get; set; }
    }

    public interface ITrendingCafe : ITrendingItem
    {
        string CafeName { get; set; }
    }


    public class TrendingItem<T> where T : ITrendingItem
    {
        public T trendItem { get; set; }
    }

    public class Cafe : ITrendingCafe
    {
        public string ItemName { get; set; }
        public string CafeName { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {
            var test = new List<TrendingItem<ITrendingItem>> { new TrendingItem<ITrendingItem> { trendItem = new Cafe() } };

            foreach (var trendingItem in test[0].trendItem.GetType().GetInterfaces())
            {
                Console.Out.WriteLine(trendingItem.Name);
            }
            Console.ReadKey();
        }
    }
}