Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# 检查子类型T的泛型对象是否为_C#_.net - Fatal编程技术网

C# 检查子类型T的泛型对象是否为

C# 检查子类型T的泛型对象是否为,c#,.net,C#,.net,我试图将下面的代码重构成一个通用函数;如果在函数声明中指定了类型,则下面的代码示例对我有效。然而,当我尝试使用T时,它失败了 我从一个外部应用程序中得到一个类似IENumerable的对象,其中包含泛型对象,我想从中筛选出那些特定类型的对象 对于上下文,这些是用户在运行代码之前在屏幕上选择的几何特征。我需要确认已经选择了正确类型的东西,并将这些东西返回到一个干净的列表中 具有已定义类型且有效的初始代码: public static List<Point> GetSelecte

我试图将下面的代码重构成一个通用函数;如果在函数声明中指定了类型,则下面的代码示例对我有效。然而,当我尝试使用T时,它失败了

我从一个外部应用程序中得到一个类似IENumerable的对象,其中包含泛型对象,我想从中筛选出那些特定类型的对象

对于上下文,这些是用户在运行代码之前在屏幕上选择的几何特征。我需要确认已经选择了正确类型的东西,并将这些东西返回到一个干净的列表中

具有已定义类型且有效的初始代码:

    public static List<Point> GetSelectedPoints()
    {
        List<Point> tmp = new List<Point>();

        Selection oSel = GetSelectionObject();

        for (int i = 1; i <= oSel.Count; i++)
        {
            try
            {
                if (oSel.Item(i).Value is Point)
                {
                    Point P = (Point)oSel.Item(i).Value;
                    tmp.Add(P);
                }
            }
            catch
            {
                throw new Exception("An error occurred whilst retrieving the selection");
            }
        }

        return tmp;
    }
因此,selection对象(同时也是从base派生的)返回一个base对象列表,它可以是……几乎任何东西

根据此函数的应用程序,我可能希望得到,例如,所有“面”或导数,或者可能只是平面面,甚至可能只是通过X定义的平面面


根据评论进行更新(mm8的荣誉指向正确的方向)

静态公共列表GetHistTypeFromSelection()
{
选择osel=GetSelectionObject();
List listA=新列表();
for(int i=1;i x).OfType().ToList();
返回结果;
}
这种方法似乎成功地过滤出了正确的对象类型-但是返回的列表仍然将它们显示为COM对象


如果您的
选择
实现了
IEnumerable
,您可以使用它来筛选所需类型的项。

如果您得到的是
System.\uu ComObject
作为类型,这个问题可能会有所帮助,那么听起来您有一些非常旧的遗留组件,您正在尝试与之交互。
GetSelectionObject()
究竟做什么并返回什么?@MindSwipe我刚刚尝试过,但没有得到任何乐趣-似乎尽管我假设,对象的“BaseType”实际上返回null。@DavidG它从外部应用程序返回一个选择对象。这没有帮助,我可以从代码中看出这一点。什么是选择对象?
    static public List<T> GetThisTypeFromSelection<T>()
    {

        Selection osel = GetSelectionObject();
        List<T> tmp= new List<T>();
        for(int i = 1; i<=osel.Count ; i++)
        {
            if (osel.Item(i).Value is T)
            {
                T thing = (T)osel.Item(i).Value;
                tmp.Add(tmp);
            }
        }
        return tmp;
    }
    public class Base
{}

public class Geometry2d : Base
{ }
public class Line : Geometry2d
{ }
public class Circle : Line
{ }
public class Face : Geometry2d
{ }
public class PlanarFace : Face
{ }
public class CylindricalFace : Face
{ }
public class PlanarFaceDefinedThroughX : PlanarFace
{ }
public class PlanarFaceDefinedThroughY : PlanarFace
{ }
etcetera...
    static public List<T> GetThisTypeFromSelection<T>()
    {

        Selection osel = GetSelectionObject();
        List<Base> listA = new List<Base>();
        for(int i = 1; i<=osel.Count ; i++)
        {
            CATBaseDispatch CbD = osel.Item(i).Value;
            listA.Add(CbD);
        }
        List<T> results = listA.Select(x => x).OfType<T>().ToList();

        return results;
    }