本文整理汇总了C#中IEnumerable.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IEnumerable.GetType方法的具体用法?C# IEnumerable.GetType怎么用?C# IEnumerable.GetType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEnumerable
的用法示例。
在下文中一共展示了IEnumerable.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EnumerableDataReader
/// <summary>
/// Create an IDataReader over an instance of IEnumerable<>.
///
/// Note: anonymous type arguments are acceptable.
///
/// Use other constructor for IEnumerable.
/// </summary>
/// <param name="collection">IEnumerable<>. For IEnumerable use other constructor and specify type.</param>
public EnumerableDataReader(IEnumerable collection)
{
foreach (Type intface in collection.GetType().GetInterfaces())
{
if (intface.IsGenericType && intface.GetGenericTypeDefinition() == typeof (IEnumerable<>))
{
_type = intface.GetGenericArguments()[0];
}
}
if (_type ==null && collection.GetType().IsGenericType)
{
_type = collection.GetType().GetGenericArguments()[0];
}
if (_type == null )
{
throw new ArgumentException(
"collection must be IEnumerable<>. Use other constructor for IEnumerable and specify type");
}
SetFields(_type);
_enumerator = collection.GetEnumerator();
}
示例2: ConvertTypes
public static IEnumerable ConvertTypes(IEnumerable source, Dictionary<string, Type> types,string typeKey="Type")
{
if (source == null) return null;
IList copy = new List<dynamic>();
if (typeof(IList).IsAssignableFrom(source.GetType()))
{
// TODO: convert arrays of double[], int[], string[], etc when appropriate
if (source.GetType().GetConstructor(Type.EmptyTypes) != null)
{
copy = Activator.CreateInstance(source.GetType()) as IList;
}
}
foreach (var value in source)
{
var childDictionary = value as IDictionary;
if (childDictionary != null)
{
copy.Add(IDictionaryExtension.ConvertTypes(childDictionary, types,typeKey));
}
else
{
var childEnumerable = value as IEnumerable;
if (childEnumerable != null && childEnumerable.GetType() != typeof(string))
{
copy.Add(ConvertTypes(childEnumerable, types));
}
else
{
copy.Add(value);
}
}
}
return Simplify(copy);
//return copy;
}
示例3: FillDropDownList
/// <summary>
/// Fills dropdownlist list with data from dataSource
/// <param name="list"></param>
/// <param name="dataSource"></param>
/// <param name="needEmpty">Indicates if we have to add empty element to dropDownList</param>
/// <param name="dataValueField">value</param>
/// <param name="dataTextField">text</param>
/// <param name="emptyText">displayed text in emptyElement </param>
public void FillDropDownList(DropDownList list, IEnumerable dataSource, String dataValueField = "", String dataTextField = "", bool needEmpty = false, String emptyText = "")
{
//if string[,] array is datasource
if(dataSource.GetType() == typeof(System.String[,]))
{
list.DataSource = dataSource;
}
else //if any List<object> is datasource
{
list.DataSource = dataSource;
}
//if value or text fields are identified
if(!string.IsNullOrEmpty(dataValueField))
{
list.DataValueField = dataValueField;
}
if(!string.IsNullOrEmpty(dataTextField))
{
list.DataTextField = dataTextField;
}
list.DataBind();
//if we have to add an empty element to dropDownList
if(needEmpty)
{
list.Items.Insert(0, new ListItem(emptyText, Constants.EpmtyDDLValue));
}
}
示例4: GetElementType
public static Type GetElementType(IEnumerable enumerable)
{
Type elementType = null;
var enumerableType = enumerable.GetType();
do
{
if (enumerableType.IsGenericType)
{
var genericArguments = enumerableType.GetGenericArguments();
if (genericArguments.Length > 0)
{
elementType = genericArguments[0];
}
}
enumerableType = enumerableType.BaseType;
} while (elementType == null && enumerableType != null);
if (elementType == null)
{
var enumItems = enumerable.GetEnumerator();
if (enumItems.MoveNext())
{
if (enumItems.Current != null)
{
elementType = enumItems.Current.GetType();
}
}
}
return elementType;
}
示例5: ItemsSourceChanged
private static void ItemsSourceChanged(BindableObject bindable, IEnumerable oldValue, IEnumerable newValue) {
var picker = (Picker)bindable;
if (picker == null)
throw new Exception("only support Picker");
var selected = picker.SelectedIndex;
var type = newValue.GetType().GenericTypeArguments[0].GetTypeInfo();
var dp = (string)bindable.GetValue(DisplayPathProperty);
PropertyInfo p = null;
if (!string.IsNullOrWhiteSpace(dp)) {
p = type.GetDeclaredProperty(dp);
}
foreach (var o in newValue) {
object value = null;
if (p != null)
value = p.GetValue(o);
else
value = o;
if (value != null)
picker.Items.Add(value.ToString());
}
picker.SelectedIndex = selected;
}
示例6: AddToCollection
public static void AddToCollection(this IEnumerable items, IEnumerable collection, Type elementType,
Type resourceType, string propertyName, Type propertyType)
{
Contract.Assert(items != null);
Contract.Assert(collection != null);
Contract.Assert(elementType != null);
Contract.Assert(resourceType != null);
Contract.Assert(propertyName != null);
Contract.Assert(propertyType != null);
MethodInfo addMethod = null;
IList list = collection as IList;
if (list == null)
{
addMethod = collection.GetType().GetMethod("Add", new Type[] { elementType });
if (addMethod == null)
{
string message = Error.Format(SRResources.CollectionShouldHaveAddMethod, propertyType.FullName, propertyName, resourceType.FullName);
throw new SerializationException(message);
}
}
else if (list.GetType().IsArray)
{
string message = Error.Format(SRResources.GetOnlyCollectionCannotBeArray, propertyName, resourceType.FullName);
throw new SerializationException(message);
}
items.AddToCollectionCore(collection, elementType, list, addMethod);
}
示例7: ToDataTableXmlString
///// <summary>
///// 序列化物件
///// </summary>
///// <typeparam name="T"></typeparam>
///// <param name="value"></param>
///// <returns></returns>
//public string ObjectToString<T>(T value)
//{
// XmlSerializer ser = new XmlSerializer(value.GetType());
// MemoryStream stream = new MemoryStream();
// ser.Serialize(stream, value);
// stream.Position = 0;
// StreamReader reader = new StreamReader(stream);
// string result = reader.ReadToEnd();
// stream.Dispose();
// reader.Dispose();
// return result;
//}
public string ToDataTableXmlString(IEnumerable value)
{
var type = value.GetType().GetGenericArguments()[0];
DataTable dataTable = new DataTable(type.Name);
PropertyInfo[] Props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
dataTable.Columns.Add(prop.Name);
}
foreach (object item in value)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
using (var memoryStream = new MemoryStream())
{
dataTable.WriteXml(memoryStream);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
return reader.ReadToEnd();
}
}
}
示例8: BuildRowData
private void BuildRowData(IEnumerable modelList, object modelItem, StringBuilder sb)
{
foreach (PropertyInfo info in modelList.GetType().GetElementType().GetProperties())
{
object value = info.GetValue(modelItem, new object[0]);
sb.AppendFormat("{0},", value);
}
}
示例9: EntityDataSourceViewSchema
/// <summary>
/// Creates a view schema with a set of typed results and an optional set of keyName properties on those results
/// </summary>
internal EntityDataSourceViewSchema(IEnumerable results, string[] keyNames)
{
Type type = GetListItemType(results.GetType());
PropertyInfo[] infos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(type);
CreateColumnsFromPropDescs(properties, keyNames);
}
示例10: BuildHeaders
private void BuildHeaders(IEnumerable modelList, StringBuilder sb)
{
foreach (PropertyInfo property in modelList.GetType().GetElementType().GetProperties())
{
sb.AppendFormat("{0},", property.Name);
}
sb.NewLine();
}
示例11: Create
public static object Create(Type resultCollectionType, MocksRepository repo, IMockReplicator replicator, IEnumerable collection)
{
if (resultCollectionType == typeof(string))
return null;
Type sourceType = collection.GetType();
if (resultCollectionType.IsAssignableFrom(sourceType))
return collection;
var enumerableType = resultCollectionType.GetImplementationOfGenericInterface(typeof(IEnumerable<>)) ?? typeof(IEnumerable);
if (!enumerableType.IsAssignableFrom(resultCollectionType))
throw new MockException("Return value is not an enumerable type.");
var elementType = enumerableType.IsGenericType ? enumerableType.GetGenericArguments()[0] : typeof(object);
var ilistType = typeof(IList<>).MakeGenericType(elementType);
var iqueryableType = typeof(IQueryable<>).MakeGenericType(elementType);
IEnumerable list;
if (typeof(ICollection).IsAssignableFrom(sourceType))
{
list = collection;
}
else
{
var listType = typeof(List<>).MakeGenericType(elementType);
var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(elementType);
var castCollection = castMethod.Invoke(null, new[] { collection });
list = (IEnumerable)MockingUtil.CreateInstance(listType, castCollection);
}
var listBehavior = new DelegatedImplementationBehavior(list,
new[]
{
ilistType,
typeof(IList),
});
var queryable = list.AsQueryable();
var queryableType = queryable.GetType();
var queryableBehavior = new DelegatedImplementationBehavior(queryable,
new[] { queryableType.GetImplementationOfGenericInterface(typeof(IQueryable<>)) });
if (replicator != null)
{
var mock = replicator.CreateSimilarMock(repo, resultCollectionType, null, true, null);
var mockMixin = MocksRepository.GetMockMixin(mock, null);
mockMixin.FallbackBehaviors.Insert(0, queryableBehavior);
mockMixin.FallbackBehaviors.Insert(0, listBehavior);
return mock;
}
else
{
return repo.Create(resultCollectionType, null, Behavior.Loose, MockingUtil.EmptyTypes, null,
null, null, new List<IBehavior> { listBehavior, queryableBehavior });
}
}
示例12: showListContents
public static void showListContents(IEnumerable list)
{
DI.log.debug("Showing contents of list of type: {0}\n", list.GetType());
int itemCount = 0;
foreach (object item in list)
DI.log.info(" [{0}] {1}", itemCount++, item.ToString());
DI.log.info("");
}
示例13: JsonContacts
private static string JsonContacts(IEnumerable<ContactInfo> contacts)
{
var serializer = new DataContractJsonSerializer(contacts.GetType());
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, contacts);
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
}
示例14: AreEqual
// ----------------------------------------------------------------------
public static bool AreEqual( IEnumerable enumerable, object obj )
{
bool equal = enumerable == obj;
if ( !equal && enumerable != null && obj != null && enumerable.GetType() == obj.GetType() )
{
equal = HaveSameContents( enumerable, obj as IEnumerable );
}
return equal;
}
示例15: GetItemDisplayPrefix
internal static string GetItemDisplayPrefix(IEnumerable collection)
{
Type elementType = ElementType(collection.GetType());
if (elementType == null)
{
return "item";
}
return elementType.Name;
}