Код: Выделить всё
class SpecificSemiStructured : SemiStructured
{
[SemiStructuredField("field_a_key", ...)]
public string? FieldA
{
get => Get();
set => Set(value);
}
[SemiStructuredField("field_b_key", ...)]
public DateTime? FieldB
{
get => Get();
set => Set(value);
}
}
abstract class SemiStructured
{
private Dictionary _values;
protected SemiStructured(string data)
{
// Data is a concatenated string containing the key-value pairs
// These are separated by a specific delimiter
_values = ParseData(data);
}
protected TValue? Get([CallerMemberName] string propertyName = "")
{
// Code for retrieving the semi-structured field from the parsed dictionary
// This checks that the property name provided is:
// 1. The name of a valid property of 'T'
// 2. The name of a property of 'T' that has a 'SemiStructuredField' attribute
// Gets the attribute data for the given property
return Get(new SemiStructuredFieldInfo(attribute.Key, ...));
}
protected TValue? Get(SemiStructuredFieldInfo fieldInfo)
{
// Retrieves corresponding value from dictionary
// Handles data conversions
// Etc
// Returns a value
}
}
Код: Выделить всё
class SpecificSemiStructured : SemiStructured
{
[SemiStructuredField("field_a_key")]
public string? FieldA { get; set; }
[SemiStructuredField("field_b_key")]
public DateTime? FieldB { get; set; }
}
Много лет назад я использовал пакет Fody's PropertyChanged для инъекции кода при создании приложения WPF/MVVM. Хотя я хотел бы здесь нечто подобное, я немного беспокоюсь о дополнительной сложности. Есть ли способ без ткачества IL? Я слышал, как люди используют генераторы источников для подобных вещей, но я считаю, что на это существуют более сильные ограничения? И я не хочу генерировать весь код, иначе наверняка нельзя ссылаться?
Подробнее здесь: https://stackoverflow.com/questions/797 ... properties
Мобильная версия