ASP与C#中引用类型的奇异性 [英] Oddity with reference type in ASP vs. C#

查看:48
本文介绍了ASP与C#中引用类型的奇异性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#编写的.Net程序集,当它被要求填充控制台应用程序与ASP.Net网页中的引用类型时,它所做的工作截然不同.

两种情况下的代码如下:

I have a .Net assembly, written in C#, that is doing radically different things when it is asked to populate a reference type in a console app vs. an ASP.Net web page.

The code in either case looks like this:

//-------In assembly

//thing is NOT IDisposable
class Thing
{
public int x; 
};

class C
{
public void foo(string s, out Thing thing)
{
thing = new Thing();
thing.x = blah(s); //cleverly turn English to integers, e.g. "three" -> 3, etc. 
}

//--------Client code, can be either C# or ASP.Net

List<thing> things = new List<thing>(); 
string[] string_array = new string_array{"zero", "one", "two",... , "five"};
C c = new C();
Thing thing = null; 
foreach(string s in string_array)
{
    c.foo(s, out thing); 
    things.Add(thing);
    thing = null;
}


因此,我通过调用fxn填充唯一的事物"列表.

当我在C#中作为控制台应用程序运行时,所有的工作都与我预期的一样:我得到了x = 0,1,2 ...等的事物"列表.

当我从ASP.Net调用相同的程序集时,将此代码嵌入到网页中,我会得到一个事物"列表,其中x始终设置为数组中的最后一个值.换句话说,程序集似乎保存了对对象的引用,并用较晚的值覆盖了较早的值.

我尝试将事物"设置为NULL等,但均无济于事.这里发生了一些深事...但是我的大脑陷入了浅水...

最大的问题是,ASP和C#为何有区别?

Eric


So I am populating a list of unique ''things'' by calling a fxn.

When I run as a console app in C#, all works as I would expect: I get a list of ''things'' with x = 0,1,2 ... etc.

When I call the same assembly from ASP.Net, with this code embedded in a web page, I get a list of ''things'' with x always set to the last value in the array. In other words, the assembly seems to save a reference to the object and overwrites early values with late ones.

I have tried setting ''thing'' to NULL, etc. etc. to no avail. Something deep is happening here ... but my brain is set on shallow ...

The Big Question is, why is this different in ASP vs. C#?

Eric

推荐答案

可能未在回发之间存储列表.

呈现页面并将其发送给客户端后,该页面(及其所有属性和变量)将不复存在.

尝试将其存储为会话状态.
You are probably not storing the list between post backs.

Once a page is rendered and sent to the client, it (and all it''s properties and variables) cease to exist.

Try storing it in Session state.


我看不到任何ASP.NET可能出问题的地方.我认为您的问题不正确或报告不完整.我应该提醒您,即使在ASP.NET中,您也可以在调试器下运行项目.

您的代码不完整,看起来也不是很好.这是我看到的:

1)C类的目的是什么?此代码非常适合作为Thing的成员.

2)您通过引用参数根本没有任何意义.返回参数是什么?无论如何,您都使用void return.为什么?!应该是:

I cannot see anything which can be wrong with ASP.NET specifically. I think your problem is incorrect or incomplete reporting of the problem. I should remind you that you can run your project under debugger even in ASP.NET.

Your code is incomplete and looks not very good. Here is what I see:

1) What''s the purpose of the class C? This code would fit well as a member of Thing.

2) You by reference parameter makes no sense at all. What''s the return parameter for? You use void return anyway. Why?! Should be:

public Thing foo(string s)
{
    thing = new Thing();
    thing.x = blah(s); 
    return thing;
}



3)最好不要使用方法blah(您是否真的在使用这样俗气的名字:-)?),而是最好使它成为Thing的构造函数.

4)如果blah未使用"this",则它应该与foo一样是静态的.如果C没有其他内容,则应为静态类.

5)方法foo应该引发异常.并非每个字符串s都可以解释为英文数字,​​对吗?

我要说的是,对于ASP.NET来说,您对简单的编程任务还不够自在.同时,我看不到任何特定于ASP.NET的东西,在这里可能特别"错误.也许,如果您的代码需要显示更多—您可能在代码的其他部分做错了,我什至无法想象...

—SA



3) Instead of having the method blah (are you really using such cheesy names :-)?), you should better make it a constructor of Thing.

4) If blah is not using "this", it should be static, as well as foo. If there is nothing else if C, it should be a static class.

5) The method foo should throw exception. Not every string s could be interpreted as English numeric, right?

I would say you''re not comfortable enough in simple programming tasks to go if for ASP.NET. At the same time, I cannot see anything specific to ASP.NET which can be "specifically" wrong here. Maybe, you need to show more if your code — you''re maybe doing something wrong in other part of the code, something I cannot even imagine…

—SA


我已经解决了这个问题,并将答案发布给他人,以便其他人可以谷歌搜索.大会持有导致问题的事物的引用.我仍然不知道为什么这在标准Windows应用程序中没有失败.

感谢所有答复.

埃里克(Eric)
I have solved this problem and will post the answer so others can Google it. The assembly was holding a reference to the Thing that was causing the issue. I still don''t know why this did not fail in the standard Windows app.

Thanks to all who replied.

Eric


这篇关于ASP与C#中引用类型的奇异性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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