PDA

View Full Version : Không thể sử dụng kiểu generic trong extension method



dieucay555
11-11-2011, 16:22
Mình viết extension method đơn giản,convert IEnumerable sang ObservableCollection. Code như sau :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data;

namespace BookStore.Helper
{
public static class ExtensionMethods<T> where T: class
{
public static ObservableCollection<T> ToObservableCollection(this IEnumerable<T> source)
{
ObservableCollection<T> target = new ObservableCollection<T>();
foreach (T item in source)
target.Add(item);
return target;
}

}
}
mà toàn bị báo lỗi "Error 5 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) "
Làm thế nào để fix lỗi này được nhỉ, mình viết trên VS2k10

238263624
14-11-2011, 19:37
namespace BookStore.Helper
{
public static class ExtensionMethods
{
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)
{
ObservableCollection<T> target = new ObservableCollection<T>();
foreach (T item in source)
target.Add(item);
return target;
}

}
}