我appsettings.json有匹配策略的價值,它是字串
我也有同名的班級
這是樣本
public class FuzzyCompanyNameStrategy : MatchingScoreCalculationStrategy
{
public override int CalculateScore(ScoreInputModel input, SupplierModel supplier) =>
Fuzz.Ratio(input.CompanyName, supplier.SupplierNameNormalized);
}
我在需要獲取此類和方法的位置進行編碼,我有輸入引數-此類的名稱,但它是字串。
這是這堂課
protected async Task<MatchingScoreOutput> FindMatch(string name, string postalCode, string city, string matchingStrategy)
{
try
{
string comparisonName = ComparablePropsHelper.GetComparableCompanyName(name);
string comparisonPostalCode = ComparablePropsHelper.GetComparablePostalCode(postalCode);
string comparisonCity = ComparablePropsHelper.GetComparableCityName(city);
var jobSuppliers = await _dataPipelineDbContext.JobSuppliers.AsNoTracking().ToListAsync();
return new MatchingScoreOutput
{
MatchFound = true,
Confidence = MatchConfidence.HIGH,
Score = ,
};
}
catch (Exception ex)
{
_logger.LogError(ex,
$"{nameof(GenericScoreSupplierComparisonService<TEntity>)}: Exception occurred. Message: {ex.Message}");
throw;
}
在 Score =我需要以某種方式呼叫來自matchingStrategy并呼叫的策略CalculateScore
我怎么能做到這一點?
uj5u.com熱心網友回復:
執行此操作的幾種方法使用 System.Reflection 獲取定義型別的程式集。Assembly 類有一個 GetType 方法,它接受一個字串并回傳型別。創建該型別的一個實體,將其轉換為 MatchingScoreCalculationStrategy 并呼叫該方法。它可以變得非常繁瑣。我個人更喜歡使用屬性,那么策略的名稱不必是類名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457463.html
