首页 新闻 会员 周边 捐助

RoutedCommand 源码中构造器赋值的一个疑问?

0
悬赏园豆:20 [已解决问题] 解决于 2019-10-17 17:02

RoutedCommand 位于 System.Windows.Input 命名空间下,唯一继承自 ICommand,

其构造方法为

  /// <summary>Initializes a new instance of the <see cref="T:System.Windows.Input.RoutedCommand" /> class.</summary>
    public RoutedCommand()
    {
      this._name = string.Empty;
      this._ownerType = (Type) null;
      this._inputGestureCollection = (InputGestureCollection) null;
    }

    /// <summary>Initializes a new instance of the <see cref="T:System.Windows.Input.RoutedCommand" /> class with the specified name and owner type.</summary>
    /// <param name="name">Declared name for serialization.</param>
    /// <param name="ownerType">The type which is registering the command.</param>
    /// <exception cref="T:System.ArgumentNullException">
    /// <paramref name="name" /> is <see langword="null" />.</exception>
    /// <exception cref="T:System.ArgumentException">the length of <paramref name="name" /> is zero.</exception>
    /// <exception cref="T:System.ArgumentException">
    /// <paramref name="ownerType" /> is <see langword="null" />.</exception>
    public RoutedCommand(string name, Type ownerType)
      : this(name, ownerType, (InputGestureCollection) null)
    {
    }

this._inputGestureCollection = (InputGestureCollection) null;

为什么 赋值 null 还需要给它强制转换成 InputGestrueCollection 类型呢? 避免装拆箱,影响性能?

WPF
BUTTERAPPLE的主页 BUTTERAPPLE | 老鸟四级 | 园豆:3190
提问于:2019-08-23 10:20
< >
分享
最佳答案
0
 #region Constructors

        /// <summary>
        ///     Default Constructor - needed to allow markup creation
        /// </summary>
        public RoutedCommand()
        {
            _name = String.Empty;
            _ownerType = null;
            _inputGestureCollection = null;
        }

        /// <summary>
        /// RoutedCommand Constructor with Name and OwnerType
        /// </summary>
        /// <param name="name">Declared Name of the RoutedCommand for Serialization</param>
        /// <param name="ownerType">Type that is registering the property</param>
        public RoutedCommand(string name, Type ownerType) : this(name, ownerType, null)
        {
        }

        /// <summary>
        /// RoutedCommand Constructor with Name and OwnerType
        /// </summary>
        /// <param name="name">Declared Name of the RoutedCommand for Serialization</param>
        /// <param name="ownerType">Type that is registering the property</param>
        /// <param name="inputGestures">Default Input Gestures associated</param>
        public RoutedCommand(string name, Type ownerType, InputGestureCollection inputGestures)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (name.Length == 0)
            {
                throw new ArgumentException(SR.Get(SRID.StringEmpty), "name");
            }

            if (ownerType == null)
            {
                throw new ArgumentNullException("ownerType");
            }

            _name = name;
            _ownerType = ownerType;
            _inputGestureCollection = inputGestures;
        }

        /// <summary>
        /// RoutedCommand Constructor with Name and OwnerType and command identifier.
        /// </summary>
        /// <param name="name">Declared Name of the RoutedCommand for Serialization</param>
        /// <param name="ownerType">Type that is registering the property</param>
        /// <param name="commandId">Byte identifier for the command assigned by the owning type</param>
        internal RoutedCommand(string name, Type ownerType, byte commandId) : this(name, ownerType, null)
        {
            _commandId = commandId;
        }

        #endregion

我这边.NET4.8源码显示和你的不一样啊

收获园豆:20
楚人Leo | 小虾三级 |园豆:803 | 2019-10-17 16:38

应该是 Resharper 自己反编译后处理过的,不是源码的问题。O(∩_∩)O哈哈~

BUTTERAPPLE | 园豆:3190 (老鸟四级) | 2019-10-17 17:02

@BUTTERAPPLE: 应该是的

楚人Leo | 园豆:803 (小虾三级) | 2019-10-19 20:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册