Analytics

顯示具有 Generic 標籤的文章。 顯示所有文章
顯示具有 Generic 標籤的文章。 顯示所有文章

2016年6月27日 星期一

[C#]使用 泛型複製物件屬性 (Copy the generic object properties)


問題
使用 泛型複製物件屬性



解決方法
public static class ObjectExt
{
     public static void CopyTo(this T1 obj, ref T2 otherObject)
            where T1 : class
            where T2 : class
        {
            PropertyInfo[] destFields = otherObject.GetType().GetProperties(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);

            PropertyInfo[] srcFields = obj.GetType().GetProperties(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);

            foreach (var property in srcFields)
            {
                var dest = destFields.FirstOrDefault(x => x.Name == property.Name);
                if (dest != null && dest.CanWrite)
                    dest.SetValue(otherObject, property.GetValue(obj, null), null);
            }
        }
}

[C#]使用 泛型產生物件Update SQL (Update SQL generated objects using generics)


問題
使用 泛型產生物件Update SQL



解決方法
internal string GetUpdateString<T>(T obj) where T : class
{
    PropertyInfo[] srcFields = obj.GetType().GetProperties(
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);

    List<string> columnString = new List<string>();
    foreach (var property in srcFields)
    {
        if (((DescriptionAttribute)((TypeDescriptor.GetProperties(this)[property.Name].Attributes)[typeof(DescriptionAttribute)])).Description == "")
        {
            if (property.GetValue(obj, null) != null)
            {
                if (property.PropertyType == typeof(decimal) ||
                    property.PropertyType == typeof(double) ||
                    property.PropertyType == typeof(int))
                {
                    columnString.Add(_getFieldName(property.Name)+ "=" + property.GetValue(obj, null).ToString());
                }
                else if (property.PropertyType == typeof(string))
                    columnString.Add(_getFieldName(property.Name)+"='" + property.GetValue(obj, null).ToString() + "'");
            }
            else
                columnString.Add(_getFieldName(property.Name) + "=" + "null");
        }

    }
    if (columnString.Count > 0)
    {
        string sql = string.Format(@"
  UPDATE  {0}
  SET {1}
  ", srcFields[0].DeclaringType.Name, string.Join(",", columnString.ToArray()));
        return sql;
    }
    else
        throw new ArgumentNullException(string.Format("發生錯誤於:{0} 物件未實體化", this.GetType().Name));
}

[C#]使用 泛型產生物件INSERT SQL (INSERT SQL generated objects using generics)


問題
使用 泛型產生物件INSERT SQL



解決方法
internal string GetInsertString<T>(T obj) where T : class
{
    PropertyInfo[] srcFields = obj.GetType().GetProperties(
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);

    List<string>columnString = new List<string();                
    List<string> valueString = new List<string();                
    foreach (var property in srcFields)
    {
        if (property.GetValue(obj, null) != null)
        {
            columnString.Add(property.Name);
            if (property.PropertyType == typeof(decimal)
           || property.PropertyType == typeof(double)
          || property.PropertyType == typeof(int))
            {
                valueString.Add(property.GetValue(obj, null).ToString());
            }
            else if (property.PropertyType == typeof(string))
            {
                valueString.Add("'" + property.GetValue(obj, null).ToString() + "'");
            }
        }
       
    }
    string sql = string.Format(@"
 INSERT INTO {0}
 ({1})
 values
 ({2})
 ", srcFields[0].DeclaringType.Name, string.Join(",", columnString.ToArray()), string.Join(",", valueString.ToArray()));
    return sql;
}

2011年6月28日 星期二

[C#]使用 List.GetRange取得泛型第n到n筆資料(Use List.GetRange get generic n-th to n document data)


問題
使用 List.GetRange取得泛型第n到n筆資料



解決方法
List<string> result = new List<string>();
result.Add(“test1”);
result.Add(“test2”);

int start=1;
int rowcount=1;
var r = result.GetRange(start, rowcount);
//r =test1;

2011年5月27日 星期五

[LINQ]使用 LINQ.ToList output List data(Use LINQ.ToList output List data)


問題
使用 LINQ.ToList output List data



解決方法
//列出執行程式目錄裡的.rpt檔
string rootPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
DirectoryInfo dr = new DirectoryInfo(rootPath);
FileInfo[] fi = dr.GetFiles();
List<string> rptList = new List<string>();
rptList = (from a in fi
where a.Extension == ".rpt"
select a.Name).ToList<string>();

2011年2月20日 星期日

[WCF]使用 List<T>傳遞資料( Use List data transfer)


問題

使用 List<T>傳遞資料
WCF上的其中一個Operation定義如下:
1
當在WCF上使用List<T>做為資料交換的參數時,如果Client端沒特別指定時,預設為System.Array
所以即便WCF 服務端定義為List<T>型別的參數,在Client端卻為Array型別
1.1
Client端檢查方式
2
3

解決方法

因此若要將之變更為List<T>的型別,需手動指定
2
4
手動指定後,就可以使用List<T>的方式傳遞參數資料
4.1

熱門文章