Здравствуйте, vaa, Вы писали:
vaa>SetNull принимает поля, но не св-ва или индексаторы.
vaa>Возможно ли что-то похожее для св-в чтобы сократить кол-во ифчиков?
vaa>vaa>class A
vaa>{
vaa> public string Name {get;set;}
vaa>}
vaa>void SetNull(ref string v) => if (v == "") v = null;
vaa>SetNull(ref a.Name);
vaa>
using System;
class A {
public string Name { get; set; }
}
public class Ref<T> {
public Func<T> get { get; private set; }
public Action<T> set { get; private set; }
Ref(Func<T> get, Action<T> set) { this.get = get; this.set = set; }
public static Ref<T> propery<Y>(Y y, string name, params object[] index) {
var p = typeof(Y).GetProperty(name);
return new Ref<T>( () => (T)p.GetValue(y, index), v => p.SetValue(y, v, index) );
}
}
class App {
static void SetNull(ref string v) { if (v == "") v = null; }
static void SetNull(Ref<string> v) { if (v.get() == "") v.set(null); }
static void Main(string[] args) {
string Name = "";
var a = new A() { Name = "" };
SetNull(ref Name);
SetNull(Ref<string>.propery(a, "Name"));
}
}