Tag: c# 3.0

在Windows窗体项目上使用DataAnnotations

我最近使用ASP.Net MVC和DataAnnotations,并且正在考虑对Forms项目使用相同的方法,但我不确定如何去做。 我已经设置了我的属性,但是当我单击“保存”时似乎没有检查它们。 更新:我使用了Steve Sanderson的方法 ,它将检查我的类的属性并返回一组错误,如下所示: try { Business b = new Business(); b.Name = “feds”; b.Description = “DFdsS”; b.CategoryID = 1; b.CountryID = 2; b.EMail = “SSDF”; var errors = DataAnnotationsValidationRunner.GetErrors(b); if (errors.Any()) throw new RulesException(errors); b.Save(); } catch(Exception ex) { } 您如何看待这种方法?

在C#中输出Unicode字符

我是编程和自学的新手。 我正在尝试输出Taurus的占星符号,它应该是Unicode中的U + 2649。 这是我正在使用的代码…… string myString = “\u2649”; byte[] unicode = System.Text.Encoding.Unicode.GetBytes(myString); Console.WriteLine(unicode.Length); 我得到的结果是数字2而不是符号或字体。 我确定我做错了什么。

如果扩展方法与密封类中的方法具有相同的签名,那么调用优先级是什么?

我正在阅读C#3.0中的扩展方法。 我正在阅读的文本暗示一个扩展方法与扩展类中的方法具有相同的签名将是执行顺序的第二个 – 也就是说,密封类中的方法被调用。 如果是这种情况,你怎么能延长密封class?

迭代过程中的exception收集和从该集合中删除项目

我在foreach循环中从ArrayList中删除项目并获得以下exception。 collections被修改; 枚举操作可能无法执行。 如何删除foreach中的项目, 编辑: 可能有一个项目要删除或两个或全部。 以下是我的代码: /* * Need to remove all items from ‘attachementsFielPath’ which does not exist in names array. */ try { string attachmentFileNames = txtAttachment.Text.Trim(); // Textbox having file names. string[] names = attachmentFileNames.Split(new char[] { ‘;’ }); int index = 0; // attachmentsFilePath is ArrayList holding full path of […]

是否可以在C#中定义通用lambda?

我在一个操作指定类型的方法中有一些逻辑,我想创建一个封装逻辑的通用lambda。 这就是我想要做的精神: public void DoSomething() { // … Func GetTypeName = () => T.GetType().Name; GetTypeName(); GetTypeName(); GetTypeName(); // … } 我知道我可以将类型作为参数传递或创建generics方法。 但我很想知道lambda是否可以定义自己的通用参数。 (所以我不是在寻找替代方案。)据我所知,C#3.0不支持这一点。

你能在C#中找到一个Active Directory用户的主要组吗?

我正在开发一个管理Active Directory中的用户帐户的应用程序。 我尽可能使用System.DirectoryServices.AccountManagement命名空间,但我无法弄清楚如何确定用户的主要组。 当我尝试删除作为用户主要组的组时,我得到一个例外。 这是我目前的代码: private void removeFromGroup(UserPrincipal userPrincipal, GroupPrincipal groupPrincipal) { TODO: Check to see if this Group is the user’s primary group. groupPrincipal.Members.Remove(userPrincipal); groupPrincipal.Save(); } 有没有办法获取用户主要组的名称,以便我可以在尝试从该组中删除用户之前进行一些validation?

C#WebClient登录网站

我正在尝试通过提供我的(正确的)用户名和密码登录网站。 这是代码: string URL = @”https://www.t-mobile.co.uk/service/your-account/login/”; string username = “a_user”; string password = “a_password”; //ServicePointManager.Expect100Continue = false; CookieAwareClient client = new CookieAwareClient(); NameValueCollection postData = new NameValueCollection(); postData.Add(“username”, username); postData.Add(“password”, password); byte[] response = client.UploadValues(URL, postData); ASCIIEncoding enc = new ASCIIEncoding(); string Source = enc.GetString(response); 但是,令人惊讶的是,它没有登录。 我刚回到登录页面。 任何帮助将不胜感激,这是我现在的头! 谢谢,吉姆 为了完整性,这里是我的WebClient类 – public class CookieAwareClient : […]

为什么“并非所有代码路径都返回一个值”,带有switch语句和枚举?

我有以下代码: public int Method(MyEnum myEnum) { switch (myEnum) { case MyEnum.Value1: return 1; case MyEnum.Value2: return 2; case MyEnum.Value3: return 3; } } public enum MyEnum { Value1, Value2, Value3 } 我得到错误: “Not all code paths return a value” 。 我不明白switch语句怎么不能跳转到指定的情况之一。 enum可以以某种方式为null吗?

使用Linq选择类的属性以返回IEnumerable

如果我有一个SortedList ,我想从该类返回一个新的IEnumerable属性,我该怎么做? 我尝试过SortedList.Select(x=>x.MyProperty, x.AnotherProperty)但它不起作用。 谢谢。

限制c#中文本框十进制输入的最佳方法

如何创建一个文本框,其中只能输入类似12.00或1231231.00或123123的数字 我已经做了很长的事情,我正在寻找最好,最快的方式。 小数分隔符也必须是特定于文化的: Application.CurrentCulture.NumberFormat.NumberDecimalSeparator