由于工作上的需要,要对Discuz!NT论坛的代码进行更改和功能扩展,结果修改完代码发布到服务器上出现:'List' is an ambiguous reference between 'Discuz.Common.Generic.List<CustomHotTopic>' and 'System.Collections.Generic.List<CustomHotTopic>'的错误。该错误的中文翻译为:“List”是“Discuz.Common.Generic.List<CustomHotTopic>”和“System.Collections.Generic.List<CustomHotTopic>”之间的不明确的引用。
备注:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
Compiler Error Message: CS0104: 'List' is an ambiguous reference between 'Discuz.Common.Generic.List<Discuz.Web.forumindex.CustomHotTopic>' and 'System.Collections.Generic.List<Discuz.Web.forumindex.CustomHotTopic>'
Source Error:
Line 499: List<CustomHotTopic> customHotTopicList = new List<CustomHotTopic>(); 
Line 500: foreach (ForumHotItemInfo forumhotiteminfo in forumhotconfiginfo.ForumHotCollection)
Source File: d:\web\dt3.6.0\aspx\1\forumindex.aspx Line: 499
后经过错误排查,原因竟然是Discuz!NT代码中重写了List泛型集合,导致List类无法明确引用。
Discuz!NT重写的List集合命名空间为Discuz.Common.Generic.List,而C#原本的List命名空间为System.Collections.Generic.List。但是在项目中往往会不留意的使用System.Collections.Generic.List,特别是在.aspx页面中声明命名空间最容易出现:
<%@ Import Namespace="Discuz.Common.Generic" %>
<%@ Import Namespace="System.Collections.Generic" %>
因为Debug不会检查到页面中的这个错误,即不会检查Import Namespace的引用,只有运行网站程序的时候才会报错,所以如果重复引用了两个List的命名空间,并在.aspx页面中使用List,马上就会报错。
首先要明确一点,由于在项目中已经有许多地方用到Discuz.Common.Generic.List。所以摒弃Discuz!NT的List是很不方便的,只能在需要使用System.Collections.Generic.List的地方声明完整的命名空间以区别开来。
比如下面这样:
  /// <summary>
  /// 使用C#的List类
  /// </summary>
  System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
这样就和Discuz.Common.Generic.List区分开来,防止引用冲突!
这里顺便发下被Discuz!NT重写的List类的源代码,代码位置在 项目\Discuz.Common\Generic.DiscuzList.cs:

源代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
namespace Discuz.Common.Generic
{
    /// <summary>
    /// 列表泛型类
    /// </summary>
    /// <typeparam name="T">占位符(下同)</typeparam>
    [Serializable]
    public class List<T> : System.Collections.Generic.List<T>, IDiscuzCollection<T> 
    {
        #region 构造函数
        public List() : base() 
        { }
		public List(IEnumerable<T> collection) : base(collection) 
        { }
        public List(int capacity) : base(capacity) 
        { }
        #endregion
        public List<T> Take(int count)
        {
            return this.GetRange(0, count < 0 ? 0 : count);
        }
        public List<T> FindAll(Predicate<T> match)
        {
            List<T> list = new List<T>();
            list.AddRange(base.FindAll(match));
            return list;
        }
  
        public new List<T> GetRange(int start, int count)
        {
            List<T> list = new List<T>();
            //对传入起始位置和长度进行规则校验
            if (start < this.Count && (start + count) <= this.Count)
            {
                list.AddRange(base.GetRange(start, count));
            }
            return list;
        }
        public object SyncRoot
        {
            get
            {
                return this;
            }
        }
        /// <summary>
        /// 是否为空
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                return this.Count == 0;
            }
        }
        private int _fixedsize = default(int);
        /// <summary>
        /// 固定大小属性
        /// </summary>
        public int FixedSize
        {
            get
            {
                return _fixedsize;
            }
            set
            {
                _fixedsize = value;
            }
        }
        /// <summary>
        /// 是否已满
        /// </summary>
        public bool IsFull
        {
            get
            {
                if ((FixedSize != default(int)) && (this.Count >= FixedSize))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 版本
        /// </summary>
        public string Version
        {
            get
            {
                return "1.0";
            }
        }
        /// <summary>
        /// 作者
        /// </summary>
        public string Author
        {
            get
            {
                return "Discuz!NT";
            }
        }
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }
        /// <summary>
        /// 追加元素
        /// </summary>
        /// <param name="value"></param>
        public new void Add(T value)
        {
            if (!this.IsFull)
            {
                base.Add(value);
            }
        }
        /// <summary>
        /// 接受指定的访问方式(访问者模式)
        /// </summary>
        /// <param name="visitor"></param>
        public void Accept(IDiscuzVisitor<T> visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException("访问器为空");
            }
            System.Collections.Generic.List<T>.Enumerator enumerator = this.GetEnumerator();
            while (enumerator.MoveNext())
            {
                visitor.Visit(enumerator.Current);
                if (visitor.HasDone)
                {
                    return;
                }
            }
        }
        /// <summary>
        /// 比较对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (obj.GetType() == this.GetType())
            {
                List<T> l = obj as List<T>;
                return this.Count.CompareTo(l.Count);
            }
            else
            {
                return this.GetType().FullName.CompareTo(obj.GetType().FullName);
            }
        }
    }
}
作者:十有三
出处:https://shiyousan.com/post/635514455674047702
版权声明:本文采用知识共享许可协议:署名-相同方式共享 4.0 国际(CC BY-SA 4.0)。欢迎转载本文,转载请声明出处或保留此段声明。