我使用反射来获取List并尝试将其传递给接收List的委托。
但是,当我使用反射时,我的列表类型是:
object {System.Collections.Generic.List<T>}
当我将它传递给代理人(泛型)时,我得到一个例外,因为它期望类型:
System.Collections.Generic.List<T>
为了确认这确实是问题所在,我直接转向List<RealTClass>
并且它有效。但是,在我的代码中,我不想做出这种不必要的演员......还因为我正在使用泛型。
问题#1 :为什么反射会将对象返回为类型:object { X }
?
问题#2 :我如何&#34;删除&#34;类型中的object { X }
部分?基本上,我需要解决这个问题....
感谢。
更新#1:一些代码......
//METHOD receives 'obj' and 'includes'
T obj
Expression<Func<T, object[]>> includes = null
...
if (res && includes != null)
{
var array = includes.Body as NewArrayExpression;
if (array != null)
{
var exps = ((IEnumerable<object>)array.Expressions).ToArray();
for (var i = 0; i < exps.Length; i++)
{
var tartetListProperty = (exps[i] as MemberExpression).Member as PropertyInfo;
var navigationPropertyForList = tartetListProperty.GetCustomAttributes(typeof(NavigationPropertyForList)) as NavigationPropertyForList[];
if (navigationPropertyForList == null || navigationPropertyForList.Length == 0) continue;
var navigationPropertyForListString = navigationPropertyForList[0].TargetPropertyName;
if (tartetListProperty == null) continue;
var list = tartetListProperty.GetValue(obj); // WHERE I USE REFLECTION TO GET THE LIST
var listOfType = list.GetType().GetGenericArguments()[0];
var repDNI = uow.GetRepositoryDeleteNotIncludedAsyncByType(listOfType);
await repDNI(list, navigationPropertyForListString, obj.Id); // THIS IS WHERE IT FAILS
if (!res) break;
}
}
}
如果我执行了正确的转换,repDNI
对象是正确的并且正常工作,我唯一的问题是获取list
,类型object { X }
围绕我的正确类型。
答案 0 :(得分:0)
我可以通过更改以下行来使其工作:
之前:var list = tartetListProperty.GetValue(obj);
之后:dynamic list = tartetListProperty.GetValue(obj);
答案 1 :(得分:-1)
您可以转换为通用列表:
@echo off
set dir=%USERPROFILE%\Desktop\LogQueue
echo /// UNZIPPING ALL FOLDERS AND DELETING ORIGINAL ZIPS ///
for /R "%dir%" %%I in ("*.zip") do (
"%ProgramFiles%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
del /s /q /f %%I )
echo /// SEARCHING FOR STRING AND SAVING TO EXPORT FILE ///
for /R "%dir%" %%G in (.) do (
pushd "%%G"
for %%H in (*.log) do find /i "error" "%%H" >> results.txt
echo /// ZIPPING %%G folder ///
for %%X in (*.*) do "c:\Program Files\7-Zip\7z.exe" a "%%X_new.zip" "%%G\"
popd )
echo/& echo Completed
timeout 5
exit /b
据我所知,这应解决您的问题,同时保留通用功能。