我使用C#作为编程语言执行自动化
我有一个Enum在表中保存列。我在每个列名称的“描述”中保存这些列的数据类型。
每个列都是可排序的,排序后我想验证特定列的排序是否正确。我在列中创建数据列表,以编程方式对其进行排序,然后在屏幕上对其进行排序,读取内容,然后使用List.SequenceEquals
我正在尝试创建一个“类型”列表(在“描述”中指定)。但看起来我无法创建运行时对象列表。
对此有任何解决方案吗?
CODE
{
public enum tranColumns
{
[Description("DateTime")]
InvoiceDate,
[Description("string")]
Customer,
[Description("string")]
InvoiceID,
[Description("string")]
LiabilityTypeDescription,
[Description("string")]
TransactionStatusDescription,
[Description("string")]
TestTransaction,
[Description("string")]
TransactionDate,
[Description("int")]
TransactionID,
[Description("string")]
SourceSystem,
[Description("string")]
ShipToGeocode,
[Description("DateTime")]
CreationTime
}
var type = MyExtensions.GetDataType(tranColumns.InvoiceDate); //Gets System.Type
List<type> list= new List<type>();
}
public static string GetEnumDescription(System.Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
public static Type GetDataType(System.Enum value)
{
switch(GetEnumDescription(value).ToLower())
{
case "datetime":
return typeof(DateTime);
case "string":
return typeof(String);
case "int":
return typeof(Int32);
default:
Utilities.log("Unkwown datatype",LogType.ErrorEntry);
return typeof(Object);
}
}
无法以这种方式创建“类型”列表!
答案 0 :(得分:5)
Type listType = typeof(List<>).MakeGenericType(type);
IList list= (IList)Activator.CreateInstance(listType);
您需要将IList.Add与对象一起使用,因为无法使用强制转换为List。我重新读了你的代码,似乎你可以编辑它并以这种方式创建列表:
public static List<T> CreateMyList<T>(System.Enum value)
{
switch(GetEnumDescription(value).ToLower())
{
case "datetime":
return new List<DateTime>();
case "string":
return new List<String>();
case "int":
return new List<Int32>();
default:
Utilities.log("Unkwown datatype",LogType.ErrorEntry);
return new List<Object>();
}
}
答案 1 :(得分:1)
Linq救援!
var type = MyExtensions.GetDataType(column as Enum);
DataTable dtAscen; //=logic to read table;
List<object> listBeforeSort = (from x in dtAscen.AsEnumerable()
select (object)Convert.ChangeType(x.Field<string>(column.ToString()), type)).ToList();
var temp = listBeforeSort.OrderBy(s => s);
if(!listBeforeSort.SequenceEqual(temp))
Utilities.log("Issue with ascending sort on column " + column.ToString(), LogType.ErrorEntry);
答案 2 :(得分:0)
您可能需要Activator.CreateInstance方法(类型) https://msdn.microsoft.com/en-us/library/wccyzw83%28v=vs.110%29.aspx
string[] instances = instanceSpec.Split(';');
Array instlist = Array.CreateInstance(typeof(object), instances.Length);
object item;
for (int i = 0; i < instances.Length; i++)
{
// create the object from the specification string
Console.WriteLine("Creating instance of: {0}", instances[i]);
item = Activator.CreateInstance(Type.GetType(instances[i]));
instlist.SetValue(item, i);
}
Console.WriteLine("\nObjects and their default values:\n");
foreach (object o in instlist)
{
Console.WriteLine("Type: {0}\nValue: {1}\nHashCode: {2}\n",
o.GetType().FullName, o.ToString(), o.GetHashCode());
}