简体   繁体   中英

How can I reference a "@typeparam TModel" field in the Blazor (sub)-component?

VS-2019 Core 5 Blazor client-side component defines a @typeparam TModel to work with the

private List<TModel> _allModelRecs { get; set; }

as follows in code that follows. Take note of (a) "ddlOtherModel_Value" and (b) the "UID_MODEL" field of the TModel class ...

// Select first 'Record'.
IDictionary<int, string> _ModelFiltersList = (Dictionary<int, string>)_ModelFiltersList.OrderBy(r => r.Value);
TModel _OtherModel = _allModelRecs.Where(r => r.**UID_MODEL** == _ModelFiltersList.First().Key);
int ddlOtherModel_Value = _OtherModel.**UID_MODEL**;

Since this component can and must work with different "TModel(s)" (Customer, Vehicle, Department...etc) I need to set the "ddlOtherModel_Value" to be the "key"-value of the TModel's instance/record. The key-field name is different for each of the TModel(s) : UID_CUSTOMER, UID_VEHICLE, UID_DEPARTMENT...etc.

In the code snippet above "UID_MODEL" is shown for this question -- it is NOT the actual key-field-name.

Here is the question... How do I code for these variations in the actual TModel's key-name in place for "UID_MODEL"?

Your comments or questions or answers are welcome. Thx John

Basically you should not. UID should be defined on the abstract TModel.

If for some reason that is out of your hands then you will need to implement some extension to get the id. For example you could implement a wrapper class or a set of static extension methods. Another option is to use reflection to get the field starting with "UID" but this can be slow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM