Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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中定义没有任何类型的属性吗?_C#_.net_Properties - Fatal编程技术网

C# 可以在C中定义没有任何类型的属性吗?

C# 可以在C中定义没有任何类型的属性吗?,c#,.net,properties,C#,.net,Properties,我有三个从B模型继承的模型,每个模型都有一个名为Id的属性,每个类型都不同 public class BaseModel { string createTime; } public class IntValue:Base { int Id; } public class LongValue:Base { long Id; } public class ShortValue:Base { short Id; } 现在,我想编写一个通用函数,根据这三个模型对样

我有三个从B模型继承的模型,每个模型都有一个名为Id的属性,每个类型都不同

public class BaseModel
{
    string createTime;
}

public class IntValue:Base
{
    int Id;
}

public class LongValue:Base
{
    long Id;
}

public class ShortValue:Base
{
    short Id;
}
现在,我想编写一个通用函数,根据这三个模型对样本执行特殊操作。在这个操作中,我将把属性Id与另一个定义为函数输入的参数进行比较。所以我必须定义三个泛型函数,并对每一个进行比较。有没有办法把这三个泛型函数变成一个函数? 我可以在BaseModel中定义没有任何类型的Id吗

public void Get<T> (List<T> items,List<int> requestItem)where T : IntValue
{
    //Do Something ...

    items.where(x=> requestItem.Contains(x.id))

    //Do Something ...
}


public void Get<T> (List<T> items,List<long> requestItem)where T : LongValue
{
    //Do Something ...

    items.where(x=> requestItem.Contains(x.id))

    //Do Something ...
}

public void Get<T> (List<T> items,List<short> requestItem)where T : ShortValue
{
    //Do Something ...

    items.where(x=> requestItem.Contains(x.id))

    //Do Something ...
}

如果我理解正确,您可以执行以下操作:

公共类基模型 { 字符串创建时间; T-Id; public void获取列表项,列表请求项 { //做点什么。。。 items.wherex=>requestItem.Containsx.id //做点什么。。。 } } 然后,您只需使用正确的泛型参数继承BaseModel:

公共类ShortValue:BaseModel { } 请注意,这可能不适用于非值类型,需要IEqualityComparer或任何其他比较策略。

您可以将BaseModel设置为通用BaseModel并创建类型为TSo的属性Id。我如何处理此项。Where X=>requestItem.Containsx.Id您可以解释您的答案吗?