相关疑难解决方法(0)

你能用"where"来要求c#中的属性吗?

我想创建一个只接受可序列化类的泛型类,可以用where约束来完成吗?

我正在寻找的概念是这样的:

public class MyClass<T> where T : //[is serializable/has the serializable attribute]
Run Code Online (Sandbox Code Playgroud)

c# generics attributes constraints where

32
推荐指数
2
解决办法
6601
查看次数

C#where关键字

在下面的代码片段(C#2.0)中:

public abstract class ObjectMapperBase< T > where T : new()
{
    internal abstract bool UpdateObject( T plainObjectOrginal,
                                         T plainObjectNew,
                                         WebMethod fwm,
                                         IDbTransaction transaction );
}
Run Code Online (Sandbox Code Playgroud)

继承人的例子:

public abstract class OracleObjectMapperBase< T > : ObjectMapperBase< T > where T : new()
{
    internal override bool UpdateObject( T plainObjectOrginal,
                                         T plainObjectNew,
                                         WebMethod fwm,
                                         IDbTransaction transaction )
    {
        // Fancy Reflection code.
    }
}
Run Code Online (Sandbox Code Playgroud)

什么是where关键词呢?

c#

9
推荐指数
3
解决办法
2万
查看次数

标签 统计

c# ×2

attributes ×1

constraints ×1

generics ×1

where ×1