如何消除对泛型方法的调用的歧义

本文关键字:调用 歧义 泛型方法 何消 | 更新日期: 2023-09-27 17:56:51

给定以下简单的控制台应用程序,该应用程序说明了通知更改属性的两种方法:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var person = new Person(){ Name = "Me" };
            person.Age = 20;
            person.Weight = 80.5F;
                    person.RandomProperty = new RandomComplexObject();
            Console.ReadKey();
        }
    }

    public class Person : BaseObject
    {
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref value, ref _name, false); }
        }
        public int Age
        {
            get { return _age; }
            set { SetProperty<int>(ref value, ref _age, true, "Age", "Weight"); }
        }
        public float Weight
        {
            get { return _weight; }
            set { SetProperty(ref value, ref _weight, true, () => Weight, () => Age); }
        }
        public RandomComplexObject RandomProperty
        {
            get { return _rco; }
            //*** the following line has the error:
            //-------------------------------------
            set { SetProperty(ref value, ref _rco, true, () => Name, () => Age, () => Weight); } 
        }
        private float _weight;
        private int _age;
        private string _name;
        private RandomComplexObject _rco;
    }
    public class BaseObject : INotifyPropertyChanged
    {
        protected void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                var body = propertyExpression.Body as MemberExpression;
                var expression = body.Expression as ConstantExpression;
                handler(expression.Value, new PropertyChangedEventArgs(body.Member.Name));
            }
        }
        private void OnPropertyChanged(string propertyName)              
        {
            var handler = PropertyChanged;
            if (handler == null)
                return;
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetProperty<T>(ref T newValue, ref T currentValue, bool notify, params string[] notifications)
        {
            if (EqualityComparer<T>.Default.Equals(newValue, currentValue))
                return false;
            currentValue = newValue;
            if (notify)
                foreach (var propertyName in notifications)
                    OnPropertyChanged(propertyName);
            return true;
        }
        protected bool SetProperty<T, TProperty>(ref T newValue, ref T currentValue, bool notify, params Expression<Func<TProperty>>[] notifications)
        {
            if (EqualityComparer<T>.Default.Equals(newValue, currentValue))
                return false;
            currentValue = newValue;
            if (notify)
                foreach (var notification in notifications)
                    OnPropertyChanged(notification);
            return true;
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class RandomComplexObject{}
}

在方法调用的行SetProperty(ref value, ref _rco, true, () => Name, () => Age, () => Weight);我遇到编译错误:

无法将 lambda 表达式转换为类型"字符串",因为它不是委托类型

直接显示在 IDE 中的错误是:

方法'bool ConsoleApplication1.BaseObject.SetProperty(ref T, ref T, bool, params Expression>[])"的类型参数无法从用法中推断出来。尝试显式指定类型参数。

如何消除对 SetProperty() 方法的调用的歧义?有没有语法更简洁的方法来写这个?

如何消除对泛型方法的调用的歧义

以下变体至少可以编译。

public RandomComplexObject RandomProperty
{
    get { return _rco; }
    set
    {
        SetProperty(
            ref value,
            ref _rco,
            true,
            () => Name,
            () => Age.ToString(),    //instead of () => Age
            () => Weight.ToString());//instead of () => Weight
    }
}

我猜,您收到的错误首先是基于编译器无法推断出TProperty的事实

protected bool SetProperty<T, TProperty>(
    ref T newValue, 
    ref T currentValue, 
    bool notify, 
    params Expression<Func<TProperty>>[] notifications)
{
    //...
}

因为它需要可变数量的 Expression<Func<TProperty>> 类型参数,并且您在那里传递了 lambdas,返回 stringintfloat 。编译器肯定无法确定,其中哪一个TProperty

Weight属性的 setter 中:

public float Weight
{
    get { return _weight; }
    set
    {
        SetProperty(ref value, ref _weight, true, () => Weight, () => Age);
    }
}

有了float型的Weightint型的Age,编译器推断TPropertyfloat,因为存在从intfloat的隐式转换。