匿名初始化问题 [英] anonymous initializer problem

查看:66
本文介绍了匿名初始化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要初始化我的班级词典(在.Net 2.0中)。我想

使其内联并采用匿名方法,因为我不使用此代码来获取其他任何内容。类似于以下内容:


private static Dictionary< string,field_fields = delegate()

{

Dictionary< string, fieldresult = new Dictionary< string,field>();

result.Add(" Application",new field(" Application"," AppName"));

...

返回结果;

};


然后我需要在其余部分使用字典值声明。


显然上面的代码没有编译错误:无法转换

匿名方法块键入

''System.Collections.Generic.Dictionary< string,Grou pDates.field>''因为它

不是委托类型。


我知道它不是 - 但是使用匿名方法初始化

变量的正确方法是什么(并且有)?

我总是可以创建一个方法返回正确的初始化

对象,但不愿意。


任何建议都表示赞赏。

解决方案

< blockquote>

" Sergey Poberezovskiy" < Se ***************** @ discussion.microsoft.comwrot e
消息新闻中的
:4B ********** ************************ @ microsof t.com ...





我需要初始化我的班级字典(在.Net 2.0中)。我想

使其内联并采用匿名方法,因为我不使用此代码来获取其他任何内容。类似于以下内容:


private static Dictionary< string,field_fields = delegate()

{

Dictionary< string, fieldresult = new Dictionary< string,field>();

result.Add(" Application",new field(" Application"," AppName"));

...

返回结果;

};


然后我需要在其余部分使用字典值

声明。


显然上面的代码没有编译错误:无法转换

匿名方法块输入

''System.Collections.Generic.Dictionary< string,Grou pDates.field>''因为



不是委托类型。


我明白它不是 - 但是用匿名方法初始化

变量的正确方法是什么(并且存在)?

我总是可以创建一个返回正确输入的方法itialized

对象,但不愿意。



C#编译器只是将初始化代码放在静态构造函数中,所以

也是你应该做的。


private static readonly Dictionary< string,field_fields;


static ClassName()

{

_fields = new Dictionary< string,field>();

_fields.Add(" Application",new field(" Application"," AppName"));

}


>

任何建议都表示赞赏。



Ben,


编译器不允许在声明部分放置可执行代码 -

所有这些代码需要在方法/属性中:-(


" Ben Voigt [C ++ MVP]"写道:


>

" Sergey Poberezovskiy"< Se ***************** @ discussion.microsoft.comwrot e
消息新闻中的
:4B ********************************** @ microsof t .com ...





我需要初始化我的班级字典(在.Net 2.0中)。我想

使其内联并采用匿名方法,因为我不使用此代码来获取其他任何内容。类似于以下内容:


private static Dictionary< string,field_fields = delegate()

{

Dictionary< string, fieldresult = new Dictionary< string,field>();

result.Add(" Application",new field(" Application"," AppName"));

...

返回结果;

};


然后我需要在其余部分使用字典值

声明。


显然上面的代码没有编译错误:无法转换

匿名方法块输入

''System.Collections.Generic.Dictionary< string,Grou pDates.field>''因为



不是委托类型。


我明白它不是 - 但是用匿名方法初始化

变量的正确方法是什么(并且存在)?

我总能创建一个方法返回正确的初始化

对象,但不愿意。



C#编译器只是将初始化代码放在静态构造函数中,所以

这也是你应该做的。


private static readonly Dictionary< string,field_fields;


static ClassName()

{

_fields = new Dictionary< string,field>();

_fields.Add(" Application",new field(" Application"," AppName"));

}



任何建议都表示赞赏。




Ben,


无视我的previos消息 - 我应该仔细阅读 - 我的不好。


虽然这是一种初始化的方法 - 我想在声明部分内完成

初始化(之前)它到达静态

构造函数)。原因是

costructor中的下一行声明了使用字典值的自定义静态数组,例如

as

private static readonly field [] _REQUIRED_COLUMNS = {_ fields [" Name1"],...};

....

private static readonly field [] _CUSTOM_COLUMNS = {_ fields [" NameN"],...};


最初我只有数组声明,然后我注意到了

次字段名称在一个数组中更改,但在ALL中没有更改。作为一个解决方案,我用
创建了一个字典来保存这些值,这样就需要在一个地方进行更改




希望它有意义

Ben Voigt [C ++ MVP]"写道:


>

" Sergey Poberezovskiy" < Se ***************** @ discussion.microsoft.comwrot e
消息新闻中的
:4B ********** ************************ @ microsof t.com ...





我需要初始化我的班级字典(在.Net 2.0中)。我想

使其内联并采用匿名方法,因为我不使用此代码来获取其他任何内容。类似于以下内容:


private static Dictionary< string,field_fields = delegate()

{

Dictionary< string, fieldresult = new Dictionary< string,field>();

result.Add(" Application",new field(" Application"," AppName"));

...

返回结果;

};


然后我需要在其余部分使用字典值

声明。


显然上面的代码没有编译错误:无法转换

匿名方法块输入

''System.Collections.Generic.Dictionary< string,Grou pDates.field>''因为



不是委托类型。


我明白它不是 - 但是用匿名方法初始化

变量的正确方法是什么(并且存在)?

我总能创建一个方法返回正确的初始化

对象,但不愿意。



C#编译器只是将初始化代码放在静态构造函数中,所以

这也是你应该做的。


private static readonly Dictionary< string,field_fields;


static ClassName()

{

_fields = new Dictionary< string,field>();

_fields.Add(" Application",new field(" Application"," AppName"));

}



任何建议都表示赞赏。




Hi,

I need to initialize my class level dictionary (in .Net 2.0). I wanted to
make it inline and employ anonymous methods as I do not use this code for
anything else. Something similar to the following:

private static Dictionary<string, field_fields = delegate()
{
Dictionary<string, fieldresult = new Dictionary<string, field>();
result.Add("Application", new field("Application", "AppName"));
...
return result;
};

And then I need to use the dictionary values in the rest of the declarations.

Apparently the code above does not compile with the error: "Cannot convert
anonymous method block to type
''System.Collections.Generic.Dictionary<string,Grou pDates.field>'' because it
is not a delegate type".

I understand that it is not - but what is the correct way to initialize a
variable with anonymous method (and is there)?
I could always create a method that would return the correct initialized
object, but would prefer not to.

Any suggestions are appreciated.

解决方案


"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:4B**********************************@microsof t.com...

Hi,

I need to initialize my class level dictionary (in .Net 2.0). I wanted to
make it inline and employ anonymous methods as I do not use this code for
anything else. Something similar to the following:

private static Dictionary<string, field_fields = delegate()
{
Dictionary<string, fieldresult = new Dictionary<string, field>();
result.Add("Application", new field("Application", "AppName"));
...
return result;
};

And then I need to use the dictionary values in the rest of the
declarations.

Apparently the code above does not compile with the error: "Cannot convert
anonymous method block to type
''System.Collections.Generic.Dictionary<string,Grou pDates.field>'' because
it
is not a delegate type".

I understand that it is not - but what is the correct way to initialize a
variable with anonymous method (and is there)?
I could always create a method that would return the correct initialized
object, but would prefer not to.

The C# compiler just puts the initializer code in the static constructor, so
that''s what you should do too.

private static readonly Dictionary<string, field_fields;

static ClassName()
{
_fields = new Dictionary<string, field>();
_fields.Add("Application", new field("Application", "AppName"));
}

>
Any suggestions are appreciated.



Ben,

Compiler does not allow putting executable code in the declaration section -
all such code needs to be inside methods/properties :-(

"Ben Voigt [C++ MVP]" wrote:

>
"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:4B**********************************@microsof t.com...

Hi,

I need to initialize my class level dictionary (in .Net 2.0). I wanted to
make it inline and employ anonymous methods as I do not use this code for
anything else. Something similar to the following:

private static Dictionary<string, field_fields = delegate()
{
Dictionary<string, fieldresult = new Dictionary<string, field>();
result.Add("Application", new field("Application", "AppName"));
...
return result;
};

And then I need to use the dictionary values in the rest of the
declarations.

Apparently the code above does not compile with the error: "Cannot convert
anonymous method block to type
''System.Collections.Generic.Dictionary<string,Grou pDates.field>'' because
it
is not a delegate type".

I understand that it is not - but what is the correct way to initialize a
variable with anonymous method (and is there)?
I could always create a method that would return the correct initialized
object, but would prefer not to.


The C# compiler just puts the initializer code in the static constructor, so
that''s what you should do too.

private static readonly Dictionary<string, field_fields;

static ClassName()
{
_fields = new Dictionary<string, field>();
_fields.Add("Application", new field("Application", "AppName"));
}


Any suggestions are appreciated.




Ben,

disregards my previos message - I should have read more carefully - my bad.

Though this is a way to initialize - I wanted to accomplish the
initialization inside the declaration section (before it gets to the static
constructor). The reason for that is that the next lines inside the
costructor declare custom static arrays that use the dictionary values, such
as
private static readonly field[] _REQUIRED_COLUMNS = {_fields["Name1"], ...};
....
private static readonly field[] _CUSTOM_COLUMNS = {_fields["NameN"], ...};

Originally I had just the arrays declarations, and then I noticed that at
times a field name was changed in one array, but not in ALL. As a solution I
created a dictionary to hold the values, so that the change needs to happen
in one place only.

Hope it makes sense
"Ben Voigt [C++ MVP]" wrote:

>
"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:4B**********************************@microsof t.com...

Hi,

I need to initialize my class level dictionary (in .Net 2.0). I wanted to
make it inline and employ anonymous methods as I do not use this code for
anything else. Something similar to the following:

private static Dictionary<string, field_fields = delegate()
{
Dictionary<string, fieldresult = new Dictionary<string, field>();
result.Add("Application", new field("Application", "AppName"));
...
return result;
};

And then I need to use the dictionary values in the rest of the
declarations.

Apparently the code above does not compile with the error: "Cannot convert
anonymous method block to type
''System.Collections.Generic.Dictionary<string,Grou pDates.field>'' because
it
is not a delegate type".

I understand that it is not - but what is the correct way to initialize a
variable with anonymous method (and is there)?
I could always create a method that would return the correct initialized
object, but would prefer not to.


The C# compiler just puts the initializer code in the static constructor, so
that''s what you should do too.

private static readonly Dictionary<string, field_fields;

static ClassName()
{
_fields = new Dictionary<string, field>();
_fields.Add("Application", new field("Application", "AppName"));
}


Any suggestions are appreciated.




这篇关于匿名初始化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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