我要疯了吗? C# / 静态修饰符
我有下面的代码,奇怪的是,如果我在同一请求中多次调用它,它会继续返回相同的值(即使filename
)不同。
我刚刚单步执行了代码,甚至 stringbytes
也完全相同(即 GetBytes(string))返回相同的值。
public static string Base64EncodeString(string filename)
{
var stringbytes = System.Text.Encoding.Default.GetBytes(filename);
return Convert.ToBase64String(stringbytes);
}
I have the below code, oddly enough it keeps on returning the same value (even though filename
) is different, if i call it more than once in the same request.
Ive just stepped through the code and even stringbytes
is exactly the same (i.e. GetBytes(string)) is returning the same value.
public static string Base64EncodeString(string filename)
{
var stringbytes = System.Text.Encoding.Default.GetBytes(filename);
return Convert.ToBase64String(stringbytes);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑你没有看到你认为你看到的东西。如果您使用不同的文件名值调用该方法,该方法不会返回相同的值...除非您使用
Encoding.Default
不支持的字符。 (我不建议使用Encoding.Default
,除非您确实想要特定于平台的编码。)I suspect you're not seeing what you think you're seeing. That method won't return the same value if you call it with different values of filename... unless you're using characters which aren't supported by
Encoding.Default
. (I wouldn't suggest usingEncoding.Default
unless you really want a platform-specific encoding.)你的代码看起来是正确的。如果这不是 Jon Skeet 建议的编码问题,我会猜测您在某处有一个静态 fileName 和/或 stringbytes 变量,并且发布的代码与原始代码不 100% 相同。
Your code looks correct. If it's not an Encoding issue as suggested by Jon Skeet i would guess that you have a static fileName and/or stringbytes variable somewhere and that the posted code is not 100% the same as the original code.
您是否检查了正在加载的文件?您可能只是复制了正在打开的文件,给了它一个不同的名称,并且没有修改内容。
Did you check the files that you are loading. It is possible that you just copied the file that you are opening, gave it a different name and did not modify the content.