我在将dynamic
参数传递给异步方法时遇到问题。编译时错误为Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<dynamic>' to 'System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task>'
。
实际上,我正在尝试记录每个数据库表列条目的条目。因此,我正在使用一种通用方法,该方法采用动态oldervalue
和newvalue
并将其插入到AuditDB中。
代码
public async Task Translog(int Id, string Action, DateTime RecordTimeStamp, dynamic oldervalue, dynamic newvalue, VIBRANT db, VibrantAuditEntities context)
{
try
{
#region FrameLog
Type typeOfMyObject;
if (oldervalue != null)
typeOfMyObject = oldervalue.GetType();
else
typeOfMyObject = newvalue.GetType();
PropertyInfo[] properties = typeOfMyObject.GetProperties();
var tasks = properties.ToList().Select(i => SetPropValuess(typeOfMyObject, i, Id, Action, RecordTimeStamp, oldervalue, newvalue, db, context));
await Task.WhenAll(tasks); // Compile error at this line
#endregion
}
catch
{
throw;
}
}
public async Task SetPropValuess(Type typeOfMyObject, PropertyInfo item, int Id, string Action, DateTime RecordTimeStamp, dynamic oldervalue, dynamic newvalue, VIBRANT db, VibrantAuditEntities context)
{
await Task.Run(() =>
{
string PropertyTypeNamespace = typeOfMyObject.GetProperty(item.Name).PropertyType.Namespace;
if (PropertyTypeNamespace != "System.Collections.Generic" && PropertyTypeNamespace != "ClassLibrary")
{
if (oldervalue != null && newvalue != null)
{
//Edit
bool chk = Comparer.Equals(oldervalue.GetType().GetProperty(item.Name).GetValue(oldervalue, null), newvalue.GetType().GetProperty(item.Name).GetValue(newvalue, null));
if (chk == false)
{
// make and add log record
AuditLog al = new AuditLog();
al.TableName = (typeOfMyObject.BaseType.Name == "Object" ? typeOfMyObject.Name : typeOfMyObject.BaseType.Name);
al.EventDateUTC = RecordTimeStamp;
al.OriginalValue = Convert.ToString(oldervalue.GetType().GetProperty(item.Name).GetValue(oldervalue, null));
al.NewValue = Convert.ToString(newvalue.GetType().GetProperty(item.Name).GetValue(newvalue, null));
al.ColumnName = item.Name;
context.AuditLogs.Add(al);
}
}
else if (oldervalue != null && newvalue == null)
{
//Delete
AuditLog al = new AuditLog();
al.TableName = (typeOfMyObject.BaseType.Name == "Object" ? typeOfMyObject.Name : typeOfMyObject.BaseType.Name);
al.EventDateUTC = RecordTimeStamp;
al.OriginalValue = Convert.ToString(typeOfMyObject.GetProperty(item.Name).GetValue(oldervalue, null));
al.NewValue = null;
al.ColumnName = item.Name;
al.EventType = Action;
context.AuditLogs.Add(al);
}
else if (oldervalue == null && newvalue != null)
{
//Create
AuditLog al = new AuditLog();
al.TableName = (typeOfMyObject.BaseType.Name == "Object" ? typeOfMyObject.Name : typeOfMyObject.BaseType.Name);
al.EventDateUTC = RecordTimeStamp;
al.OriginalValue = null;
al.NewValue = Convert.ToString(typeOfMyObject.GetProperty(item.Name).GetValue(newvalue, null));
al.ColumnName = item.Name;
context.AuditLogs.Add(al);
}
}
});
}
答案 0 :(得分:0)
这很奇怪,由于某种原因,存在“动力”类型参数的事实会影响“任务”的类型。如果删除了动态参数,那么它将起作用。作为一种解决方法,您可以添加强制类型转换,但了解为什么会发生这种情况将很有趣:
var tasks = properties
.ToList()
.Select(i => SetPropValuess(typeOfMyObject, i, Id, Action, RecordTimeStamp, oldervalue, newvalue, db, context))
.Cast<Task>();