簡體   English   中英

如何在運行時通過反射獲得過載?

[英]How do I get the overload through reflection at runtime?

由於我工作的地方迫使我使用存儲過程,而且我還沒有足夠長的時間來阻止它,所以我為所有存儲過程定制了LiNQ to SQL代碼生成。 是的,沒錯,我們已經為所有數據訪問提供了存儲過程,並且由於我們沒有表訪問權限,這意味着我們經常為所有“實體”使用3個類。 現在,當我碰壁時,我即將完成最新的更改。

以下代碼不起作用,原因是它正在調用自身,但是我需要獲取重載。 原因是無法跟蹤40個參數,因此在項目構建時會生成“動態”類。

[Function(Name="dbo.user_insert")]
public IList<ProcedureResult> UserInsert(UserInsertObj userInsertObj)
{
    IExecuteResult result = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod(), 
        userInsertObj.UserId, userInsertObj.ProductId, userInsertObj.FromIp, 
        userInsertObj.CampaignName, userInsertObj.Campaign, userInsertObj.Login, 
        userInsertObj.Password, userInsertObj.Nickname, userInsertObj.Pin, userInsertObj.Language, 
        userInsertObj.Currency, userInsertObj.Country, userInsertObj.Region, userInsertObj.Birthday, 
        userInsertObj.FirstName, userInsertObj.MiddleName, userInsertObj.LastName, 
        userInsertObj.Address1, userInsertObj.Address2, userInsertObj.Address3, userInsertObj.Zip, 
        userInsertObj.City, userInsertObj.Timezone, userInsertObj.Email, userInsertObj.EmailNotify, 
        userInsertObj.Gender, userInsertObj.Phone, userInsertObj.MobileCc, userInsertObj.MobileNr, 
        userInsertObj.MobileModel, userInsertObj.Referral, userInsertObj.GroupId, 
        userInsertObj.StreetNumber, userInsertObj.StreetName);

    return new List<ProcedureResult> 
    { 
        new ProcedureResult
        {
            Name = "userId",
            Value = result.GetParameterValue(0),
            Type = typeof(System.Nullable<System.Int32>) 
        }
    };
}

我嘗試使用以下內容,但不知道要使用哪個重載並搜索MSDN,但我還沒有找到任何有用的東西。

((MethodInfo)MethodInfo.GetCurrentMethod()).DeclaringType.GetMethod("", new Type[] {})

如何獲得CurrentMethod的重載?

編輯:闡明了我們不允許使用數據庫表。

我忘記了linq! 在這種特殊情況下,我只有兩種方法,其中一種包含1個參數,而另一種包含所有其他參數,因此一個簡單的方法(見下文)可以很好地工作:

method.DeclaringType.GetMethods()
    .Where(x => x.Name == "UserInsert" 
           && x.GetParameters().Count() > 1)
    .Single()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM