![](/img/trans.png)
[英]I get Object Reference not set to an instance of an object while invoking SetExpressCheckout method
[英]Object reference not set to an instance of an object when Invoking generic reflective method
因此,這個問題非常令人困惑。 它相當復雜和抽象,所以我將盡力解釋它。
我有3類LinkedIn
, Facebook
和Twitter
。 它們全部都繼承自一個通用的基類SocialBase
,該基類僅具有一個屬性: uuid
並且僅使用此屬性,以便我們可以在數據庫中找到實際的類型。
因此,我有一個函數可以接受一個Profile
類,該類包含指向所有LinkedIn
, Facebook
和Twitter
表的外鍵,以及一個Enum值,該值將告訴我們是否要查找LinkedIn
, Facebook
或Twitter
外鍵
public static async Task UnlinkSocialAccountFromProfile(Profile prof, SocialNetworks provider)
{
//TYPE variable of LinkedIn, Facebook, or Twitter
var handler = HandlerMapping[provider];
//client is MobileServiceClient
var method = client.GetType().GetMethod("GetTable", Type.EmptyTypes);
//MobileServiceClient.GetTable<handler>()
var generic = method.MakeGenericMethod(handler);
//IMobileServiceTable<handler>
var table = generic.Invoke(client, null);
//Profile has 3 foreign keys, LinkedinUUID, FacebookUUID, TwitterUUID, we want the <handler>UUID
string propertyValue = prof.GetType().GetProperty(handler.Name + "UUID").GetValue(prof) as string;
//Invoke Extension method with our generic types
var genMethod =
typeof (Extensions).GetMethod("FilterByNamedProperty")
.MakeGenericMethod(table.GetType().GetGenericArguments()[0], propertyValue.GetType());
//Get the List<handler> that results from our query
var result = await (Task<List<Linkedin>>)(genMethod.Invoke(null, new [] {table, "uuid", propertyValue}));
//var result = await (table as IMobileServiceTable<SocialBase>).FilterByNamedProperty("uuid", propertyValue);
await new SocialResources().DeleteIfExists((result as IList<SocialBase>)[0]);
}
因此,我在這里正在獲取類的Type
,因此這將是SocialBase
子類之一。 對於這個特定的例子,我知道我要尋找LinkedIn
類型。 因此,在獲得類型LinkedIn
之后,我需要從我的MobileServiceTable
調用通用方法,因此通常看起來像MobileServiceTable.GetTable<LinkedIn>()
但是由於反思,我們不得不采取更長的路徑。
得到我返回的IMobileServiceTable<LinkedIn>
實例后,我得到了我要尋找的外鍵的值。 在這種情況下,它將稱為LinkedInUUID
。 現在來了棘手的部分。 我有這個擴展方法,它將為我構造查詢表達式,因為它必須是Expression<Func<LinkedIn, bool>>
public async static Task<List<TSource>> FilterByNamedProperty<TSource, TValue>(this IMobileServiceTable<TSource> source, string propertyName, TValue value)
{
// uuid
var property = typeof(TSource).GetProperty(propertyName);
// (TSource)p
var parExp = Expression.Parameter(typeof(TSource));
//p.uuid
var methodExp = Expression.Property(parExp, property);
// value
var constExp = Expression.Constant(value, typeof(TValue));
// p.uuid == value
var binExp = Expression.Equal(methodExp, constExp);
// p => p.uuid == value
var lambda = Expression.Lambda<Func<TSource, bool>>(binExp, parExp);
return await source.Where(lambda).ToListAsync();
}
我相信這些注釋在解釋每條語句出現時的構建過程方面做得很好。 但是,一旦我們到達return await ...
,應用程序將崩潰。 這是該行之前的輸出以及緊隨其后的錯誤。
IMobileServiceTable<TSource> source = {Microsoft.WindowsAzure.MobileServices.MobileServiceTable<SocialConnect.Linkedin>}
(正確)
propertyName = uuid
(正確)
TValue value = dscRJQSIxJaEfd
(正確)
我強烈感覺問題出在我的lambda
表達式上,但是如果我用這一行測試擴展,則var test = await (source as IMobileServiceTable<Linkedin>).Where(p => p.uuid == (value as string)).ToListAsync();
它工作得很好。 但是,一旦我將其更改為使用lambda
變量,我就會得到異常。 lambda表達式的實際值為{Param_0 => (Param_0.uuid == "dscRJQSIxJaEfd")}
,看起來正確
有任何想法嗎?
編輯抱歉,這是堆棧跟蹤的實際異常
`A first chance exception of type 'System.NullReferenceException' occurred in mscorlib.dll
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.GetTableMemberName(Expression expression, MobileServiceContractResolver contractResolver)
at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.VisitMemberAccess(MemberExpression expression)
at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.Visit(Expression node)
at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.VisitBinary(BinaryExpression expression)
at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.Visit(Expression node)
at Microsoft.WindowsAzure.MobileServices.Query.FilterBuildingExpressionVisitor.Compile(Expression expression, MobileServiceContractResolver contractResolver)
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.AddFilter(MethodCallExpression expression)
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.VisitMethodCall(MethodCallExpression expression)
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.Visit(Expression expression)
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryTranslator1.Translate()
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.Compile[T](IMobileServiceTableQuery1 query)
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.<Execute>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQuery1.<ToListAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at SocialConnect.Models.Extensions.<FilterByNamedProperty>d__22.MoveNext() in MobileServiceSample\MobileServiceSample\Extensions.cs:line 55`
好吧,我終於解決了這個問題。 顯然問題不在於查詢或我在做什么,而是與SocialBase
類有關。 我一直在使用它來制作方法,使我知道通用類型TEntity
具有uuid
屬性,因此我可以在任何數據庫中輕松找到它。 但是我想該字段沒有正確序列化,這就是導致NullReferenceException
。 刪除基類並反射性地找到屬性和值之后,一切開始正常運行。
我不明白為什么它之前無法正常工作,因為在此之前,我已經在很多情況下使用了SocialBase
基類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.