.Net的泛型限制 [英] Generics limitation on .Net

查看:65
本文介绍了.Net的泛型限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据类型在一般的

类中初始化一个类型参数。

我试图使用C ++模板表格如下,但它没有不行。

这似乎是泛型与C ++模板的限制。

有没有人知道解决方法来做到这一点? Thx:


公共课C< T>

{

私人T myValue;


private void InitializeValue(out Byte value){value = Byte.MinValue; } $ / $
private void InitializeValue(out Char value){value = Char.MinValue; } $ / $
private void InitializeValue(输出Int16值){value = Int16.MinValue; }

// ......依此类推......

private void InitializeValue(out String value){value =" Initial value" ;; } $ / $
private void InitializeValue(out Object value){value = this; }


public C()

{

InitializeValue(out myValue);

}


// ...

}

I need to initialise a typed parameter depending of its type in a generic
class.
I have tried to use the C++ template form as follow, but it doesn''t work.
It seems to be a limitation of generics vs C++ templates.
Does anyone knows a workaround to do this ? Thx :

public class C<T>
{
private T myValue;

private void InitializeValue(out Byte value) { value = Byte.MinValue; }
private void InitializeValue(out Char value) { value = Char.MinValue; }
private void InitializeValue(out Int16 value) { value = Int16.MinValue; }
// ... and so on...
private void InitializeValue(out String value) { value = "Initial value"; }
private void InitializeValue(out Object value) { value = this; }

public C()
{
InitializeValue(out myValue);
}

// ...
}

推荐答案

" Luc Vaillant" ; <路********* @ discussions.microsoft.com> écritdansle

message de news: A6 ********* @ microsoft.com ...


|我需要根据类型在通用中初始化一个类型参数

|上课。

|我尝试使用C ++模板表单如下,但它不起作用。

|它似乎是泛型与C ++模板的限制。

|有谁知道这样做的解决方法? Thx:

|

|公共等级C< T>

| {

|私人T myValue;

|

| private void InitializeValue(out Byte value){value = Byte.MinValue; }

| private void InitializeValue(out Char value){value = Char.MinValue; }

| private void InitializeValue(out Int16 value){value =

Int16.MinValue; }

| // ......依此类推......

| private void InitializeValue(out String value){value =" Initial

value" ;; }

| private void InitializeValue(out Object value){value = this; }

|

|公共C()

| {

| InitializeValue(out myValue);

| }

|

| // ...

| }


您没有指定要初始化的类型到

构造函数。


In无论如何,你可以使用''default''关键字初始化传递的任何类型

作为泛型类的参数:那么你甚至不需要声明一个

构造函数用于此目的。


公共类C< T>

{

private T myValue = default(T );

}


Joanna


-

Joanna Carter [TeamB ]

顾问软件工程师
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: A6**********************************@microsoft.com...

|I need to initialise a typed parameter depending of its type in a generic
| class.
| I have tried to use the C++ template form as follow, but it doesn''t work.
| It seems to be a limitation of generics vs C++ templates.
| Does anyone knows a workaround to do this ? Thx :
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out Byte value) { value = Byte.MinValue; }
| private void InitializeValue(out Char value) { value = Char.MinValue; }
| private void InitializeValue(out Int16 value) { value =
Int16.MinValue; }
| // ... and so on...
| private void InitializeValue(out String value) { value = "Initial
value"; }
| private void InitializeValue(out Object value) { value = this; }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }

You are not specifying the type that you wish to initialise, to the
constructor.

In any case, you can use the ''default'' keyword to initialise any type passed
as the parameter to the generic class :Then you don''t even have to declare a
constructor for this purpose.

public class C<T>
{
private T myValue = default(T);
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


是的,这是默认值,但如果我想初始化我的
$ b怎么办? $ b变量的值不是默认值?


" Joanna Carter [TeamB]"写道:
Yes, this is for the default value, but what if I want to initialize my
variable with a value that is not the default one ?

"Joanna Carter [TeamB]" wrote:
" Luc Vaillant" <路********* @ discussions.microsoft.com> a ??? crit dans le
message de news: A6 ************** @微软。 com ...

|我需要根据类型在通用中初始化一个类型参数
|上课。
|我尝试使用C ++模板表单如下,但它不起作用。
|它似乎是泛型与C ++模板的限制。
|有谁知道这样做的解决方法?谢谢:
|
|公共课C< T>
| {
|私人T myValue;
|
| private void InitializeValue(out Byte value){value = Byte.MinValue; }
| private void InitializeValue(out Char value){value = Char.MinValue; }
| private void InitializeValue(out Int16 value){value =
Int16.MinValue; }
| // ......依此类推......
| private void InitializeValue(out String value){value =" Initial
value" ;; }
| private void InitializeValue(out Object value){value = this; }
|
|公共C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
|你没有指定你想要初始化的类型到
构造函数。

无论如何,你可以使用''default''初始化传递的任何类型的关键字
作为泛型类的参数:那么你甚至不必为此目的声明一个
构造函数。

公共类C< T>
{私人T myValue =默认(T);
}
Joanna

-
Joanna Carter [TeamB]
顾问软件工程师
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a ??crit dans le
message de news: A6**********************************@microsoft.com...

|I need to initialise a typed parameter depending of its type in a generic
| class.
| I have tried to use the C++ template form as follow, but it doesn''t work.
| It seems to be a limitation of generics vs C++ templates.
| Does anyone knows a workaround to do this ? Thx :
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out Byte value) { value = Byte.MinValue; }
| private void InitializeValue(out Char value) { value = Char.MinValue; }
| private void InitializeValue(out Int16 value) { value =
Int16.MinValue; }
| // ... and so on...
| private void InitializeValue(out String value) { value = "Initial
value"; }
| private void InitializeValue(out Object value) { value = this; }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }

You are not specifying the type that you wish to initialise, to the
constructor.

In any case, you can use the ''default'' keyword to initialise any type passed
as the parameter to the generic class :Then you don''t even have to declare a
constructor for this purpose.

public class C<T>
{
private T myValue = default(T);
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer



然后你必须在T上进行类型比较,然后设置你的值<根据T的类型,


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Luc Vaillant" ; <路********* @ discussions.microsoft.com>在消息中写道

新闻:DE ********************************** @ microsof t.com ...
Then you will have to do a type comparison on T and then set your value
based on the type of T.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
是的,这是默认值,但如果我想用非默认值初始化我的
变量怎么办?

Joanna Carter [TeamB]"写道:
Yes, this is for the default value, but what if I want to initialize my
variable with a value that is not the default one ?

"Joanna Carter [TeamB]" wrote:
" Luc Vaillant" <路********* @ discussions.microsoft.com>一个écritdansle
消息de news: A6 ********************************** @ microsoft.com ...

|我需要根据类型初始化一个打字参数
通用
|上课。
|我尝试使用C ++模板表单如下,但它没有工作。
|它似乎是泛型与C ++模板的限制。
|有谁知道这样做的解决方法?谢谢:
|
|公共课C< T>
| {
|私人T myValue;
|
| private void InitializeValue(out Byte value){value =
Byte.MinValue; }
| private void InitializeValue(out Char value){value =
Char.MinValue; }
| private void InitializeValue(out Int16 value){value =
Int16.MinValue; }
| // ......依此类推......
| private void InitializeValue(out String value){value =" Initial
value" ;; }
| private void InitializeValue(out Object value){value = this; }
|
|公共C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
|你没有指定你想要初始化的类型到
构造函数。

无论如何,你可以使用''default''初始化任何类型的关键字
作为参数传递给通用类:那么你甚至不必为此目的声明一个
构造函数。

公共课C< T>
{私人T myValue =默认(T);
}

乔安娜

-
Joanna Carter [TeamB]
顾问软件工程师
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: A6**********************************@microsoft.com...

|I need to initialise a typed parameter depending of its type in a
generic
| class.
| I have tried to use the C++ template form as follow, but it doesn''t
work.
| It seems to be a limitation of generics vs C++ templates.
| Does anyone knows a workaround to do this ? Thx :
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out Byte value) { value =
Byte.MinValue; }
| private void InitializeValue(out Char value) { value =
Char.MinValue; }
| private void InitializeValue(out Int16 value) { value =
Int16.MinValue; }
| // ... and so on...
| private void InitializeValue(out String value) { value = "Initial
value"; }
| private void InitializeValue(out Object value) { value = this; }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }

You are not specifying the type that you wish to initialise, to the
constructor.

In any case, you can use the ''default'' keyword to initialise any type
passed
as the parameter to the generic class :Then you don''t even have to
declare a
constructor for this purpose.

public class C<T>
{
private T myValue = default(T);
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer



这篇关于.Net的泛型限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆