第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在運行時訪問 DisplayClass 對象的屬性

在運行時訪問 DisplayClass 對象的屬性

C#
慕虎7371278 2022-10-23 10:18:41
環(huán)境: VS2017,針對 .NET Core 2.1 的 C# 7.0。我正在嘗試編寫一個用于單元測試的假/模擬類,并且我在訪問DisplayClass對象上的屬性時遇到了困難,我知道這是來自匿名函數(shù)的閉包類。我的代碼如下所示:namespace MyCompany.Application.Web.Test.Fakes{    public class FakeElasticClient : IElasticClient    {        public FakeElasticClient() { }        public Task<ISearchResponse<T>> SearchAsync<T>(Func<SearchDescriptor<T>, ISearchRequest> selector = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class        {            var methodName = selector.Method.Name;            dynamic tgt = selector.Target;            object id = tgt?.GetType().GetProperty("id")?.GetValue(tgt, null);            int? total;            if (methodName.Contains("Quux"))            {                total = CountSomething((Guid)id);            }            else            {                total = CountSomethingElse((Guid)id);            }            return Task.Run(() => new FakeSearchResponse<T>(total ?? 0) as ISearchResponse<T>);        }        // … below here follows a couple thousand other methods即使 Visual Studio 調試器顯示該對象具有一個屬性,強制轉換id為也會Guid拋出一個:NullReferenceExceptiontgtid注釋:我從這個答案object id = …中偷了線。tgt.GetType()返回MyCompany.Application.Domain.Repository.Implementations.BusinessEntityRepository.<>c__DisplayClass8_0tgt.GetType().GetProperties()返回一個空PropertyInfo數(shù)組。受這篇博文的啟發(fā),我為 the和class添加了一個[assembly: InternalsVisibleTo("MyCompany.Application.Web.Test")]屬性,據(jù)我所知,這是調用鏈中唯一涉及的兩個類。沒有任何區(qū)別。MyCompany.Application.Domain.Repository.Implementations.BusinessEntityRepositoryMyCompany.Application.Web.Controllers我嘗試IgnoresAccessChecksTo 按照此處描述的方法進行操作,但無法編譯任何內容。如何檢索 的值id?
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

編譯器使用字段而不是屬性生成閉包類型,字段id也是如此。改為使用tgt.GetType().GetFields()



查看完整回答
反對 回復 2022-10-23
  • 1 回答
  • 0 關注
  • 93 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號