Mar 9

都是时间惹的祸 不指定

kcao , 11:59 , 技术 , 评论(0) , 引用(0) , 阅读(1609) , Via 本站原创

这程序在我这儿一切都运行得好好地,但是一换到DOUG那儿就有问题.迟迟解决不了.原来的程序没写LOG,现在加上去了一看才知道,原来是GB的时间格式和我这儿不一样,原来用的DATETIME.PARSE在他那儿就过不去了.

改成用ParseExact加上格式限制就好了,代码如下:

C# Code Copy Code To Clipboard
  1. DateTime.ParseExact("12/30/1899","M/d/yyyy",null);  

程序里有好几处类似的地方,要注意全部改到.

另外增加了LOG类,用来输出日志消息.

Tags: , ,
Feb 28

用C#读取LDAP记录 不指定

kcao , 17:05 , 技术 , 评论(0) , 引用(0) , 阅读(6527) , Via 本站原创

 

从LDAP读取记录,把edmworkstation和displayname这2项内容取出,放在一个dictionary中以备用.edmworkstation是用户的登记计算机名,可能含有多条记录.如果读取失败,则在dictionary中放一个"NotValid=yes"项目.

C# Code Copy Code To Clipboard
  1. private void GetLDAPInfo()   
  2.         {   
  3.             try  
  4.             {   
  5.                 DirectoryEntry entry = new DirectoryEntry("LDAP://ldap.xxx.com/o=xxx,c=an");   
  6.                 entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;   
  7.                 DirectorySearcher searcher = new DirectorySearcher(entry);   
  8.                 searcher.Filter = "(alias=" + getLoginName() + ')';   
  9.                 SearchResult result = searcher.FindOne();   
  10.                 if (result == null)   
  11.                 {   
  12.                     LDAPInfo.Add("NotValid""yes");   
  13.                     errnum = -40;   
  14.                     return;   
  15.                 }   
  16.   
  17.                 string path = result.Path;   
  18.                 path = path.Substring(path.LastIndexOf("/") + 1);   
  19.                 ResultPropertyCollection p = result.Properties;    
  20.   
  21.                 string v = "";                   
  22.                 if (p.Contains("edmworkstation"))   
  23.                 {   
  24.                     foreach (var a in p["edmworkstation"])   
  25.                     {   
  26.                         v += a.ToString().Substring(0, a.ToString().IndexOf('/')) + ";";   
  27.                     }   
  28.                     LDAPInfo.Add("pcnames", v);   
  29.                 }   
  30.                 else  
  31.                 {   
  32.                     LDAPInfo.Add("NotValid""yes");   
  33.                 }                   
  34.   
  35.                 if (p.Contains("displayName"))   
  36.                 {   
  37.                     v = "";   
  38.                     foreach (var a in p["displayName"])   
  39.                     {   
  40.                         v += a.ToString();   
  41.                     }   
  42.                     LDAPInfo.Add("displayname",v);      
  43.                 }   
  44.             }   
  45.             catch  
  46.             {   
  47.                 LDAPInfo.Add("NotValid""yes");   
  48.                 errnum = -40;   
  49.                 return;   
  50.             }   
  51.         }  

 

Tags: , ,
Feb 25

有一个字符串,保存着一系列的机器名,要比较某个机器名是不是在这个字符串中, 此字符串以";"分隔,但是分号前后有可能会有空格. 先用SPLIT函数分割字符串,再用Array.IndexOf进行查找比较.

C# Code Copy Code To Clipboard
  1. string source = "a; ab; abc ; bc ;b";   
  2. string unit = "bc";   
  3. string[] sarray = source.ToLower().Split(new char[] {';',' '}, StringSplitOptions.RemoveEmptyEntries);   
  4. return (Array.IndexOf<string>(sarray, unit.ToLower()) >= 0);  

 

Tags: , , ,
Jan 28

基准日期: 12/30/1899

Tags: ,
Jan 26

Generic是Framework 2.0的新元素,中文名字称之为“泛型” (我总是记不住这个名字 = =+)特征是一个带有尖括号的类,比如List<T>

在C#中,泛型用得最广泛的地方,就是集合(Collection)中。实际上,泛型的产生其中一个原因就是为了解决原来集合类中元素的装箱和拆箱问题(如果对装箱和拆箱概念不明,请百度搜索)。由于泛型的使用,使得集合内所有元素都属于同一类,这就把类型不同的隐患消灭在编译阶段——如果类型不对,则编译错误。

这里只讨论自定义泛型类。基本自定义如下:

C# 代码复制内容到剪贴板
  1. public class MyGeneric <T>   
  2. {   
  3.     private T member;   
  4.     public void Method (T obj)   
  5.     {   
  6.     }   
  7. }  

这里,定义了一个泛型类,其中的T作为一个类,可以在定义的类中使用。当然,要定义多个泛型类,也没有问题。

C# 代码复制内容到剪贴板
  1. public class MyGeneric <TKey, TValue>   
  2. {   
  3.      private TKey key;   
  4.      private TValue value;   
  5.   
  6.      public void Method (TKey k, TValue v)   
  7.      {   
  8.      }   
  9. }  

泛型的初始化:泛型是需要进行初始化的。使用T doc = default(T)以后,系统会自动为泛型进行初始化。

限制:如果我们知道,这个将要传入的范性类T,必定具有某些的属性,那么我们就可以在MyGeneric<T>中使用T的这些属性。这一点,是通过interface来实现的。

C# 代码复制内容到剪贴板
  1. // 先定义一个interface   
  2. public interface IDocument   
  3. {   
  4.    string Title ...{get;}   
  5.    string Content ...{get;}   
  6. }   
  7.   
  8. // 让范型类T实现这个interface   
  9. public class MyGeneric <T>   
  10. where T : IDocument   
  11. {   
  12.      public void Method(T v)   
  13.      {   
  14.           Console.WriteLine(v.Title);   
  15.      }   
  16. }   
  17.   
  18. // 传入的类也必须实现interface   
  19. public class Document : IDocument   
  20. {   
  21. ......   
  22. }   
  23.   
  24. // 使用这个泛型   
  25. MyGeneric<Document> doc = new MyGeneric<Document>();   

泛型方法:我们同样可以定义泛型的方法

C# 代码复制内容到剪贴板
  1. void Swap<T> (ref T x, ref T y)   
  2. {   
  3. T temp = x;   
  4. x = y;   
  5. y = temp;   
  6. }  

泛型代理(Generic Delegate):既然能够定义泛型方法,自然也可以定义泛型代理

C# 代码复制内容到剪贴板
  1. public delegate void delegateSample <T> (ref T x, ref T y)   
  2.   
  3. private void Swap (ref T x, ref T y)   
  4. {   
  5.     T temp = x;   
  6.     x = y;   
  7.     y = temp;   
  8. }   
  9.   
  10. // 调用   
  11. public void Run()   
  12. {   
  13.    int i,j;   
  14.    i = 3;   
  15.    j = 5;   
  16.    delegateSample<int> sample = new delegateSample<int> (Swap);   
  17.    sample(i, j);   
  18. }  

设置可空值类型:一般来说,值类型的变量是非空的。但是,Nullable<T>可以解决这个问题。

C# 代码复制内容到剪贴板
  1. Nullable<int> x;   // 这样就设置了一个可空的整数变量x   
  2. x = 4;   
  3. x += 3;   
  4. if (x.HasValue)   // 使用HasValue属性来检查x是否为空   
  5. {Console.WriteLine ("x="+x.ToString());   
  6. }   
  7. x = null;    // 可设空值  

使用ArraySegment<T>来获得数组的一部分。如果要使用一个数组的部分元素,直接使用ArraySegment来圈定不失为一个不错的办法。

C# 代码复制内容到剪贴板
  1. int[] arr = ...{1, 2, 3, 4, 5, 6, 7, 8, 9};   
  2. // 第一个参数是传递数组,第二个参数是起始段在数组内的偏移,第三个参数是要取连续多少个数   
  3. ArraySegment<int> segment = new ArraySegment<int>(arr, 2, 3);  // (array, offset, count)    
  4.   
  5. for (int i = segment.Offset; i<= segment.Offset + segment.Count; i++)   
  6. {   
  7.    Console.WriteLine(segment.Array[i]);    // 使用Array属性来访问传递的数组   
  8. }  

在例子中,通过将Offset属性和Count属性设置为不同的值,可以达到访问不同段的目的。

(转自: http://blog.csdn.net/ezhuyin/archive/2007/10/05/1812312.aspx)

 

Tags: , , , , ,
分页: 8/20 第一页 上页 3 4 5 6 7 8 9 10 11 12 下页 最后页 [ 显示模式: 摘要 | 列表 ]