嗨,我已经尽最大努力在 Google 上进行了搜索,但无法从这些错误中恢复:
The name 'DateTimeStyles' does not exist in the current context
...
The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
..你能帮我解决一下吗,如果我错过了什么..这是我的代码..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Security.Permissions;
using System.Threading;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
"5/1/2009 6:32:00", "05/01/2009 06:32",
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"};
DateTime dateValue;
foreach (string dateString in dateStrings)
{
if (DateTime.TryParseExact(dateString, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out dateValue))
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
else
Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Visual Studio 2005。
看起来你的 using 语句没问题。一定缺少一个参考。错误消息中提到的两种类型都存在于mscorlib.dll.
我相信 C# 编译器总是引用 mscorlib,除非您要求它不要使用/nostdlib命令行选项。
你如何编译你的程序?如果使用 Visual Studio,请检查项目设置中的“不使用 stdlib”选项或类似选项(另一个答案解释了在哪里可以找到该选项)。如果使用命令行编译器,请确保您没有传递该/nostdlib选项。