IHttpActionResult当我的WebApi GET操作中找不到某些内容时,我将返回NotFound .除了这个响应,我想发送自定义消息和/或异常消息(如果有的话).当前ApiController的NotFound()方法不提供传递消息的重载.
有没有办法做到这一点?或者我将不得不写自己的习惯IHttpActionResult?
我正在尝试List<KeyValuePair<string,int>>通过覆盖索引器来添加查找元素的功能.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    public class MyList : List<KeyValuePair<string, int>>
    {
        public int this[string key]
        {
            get
            {
                return base.Single(item => item.Key == key).Value;
            }
        }
    }
}
出于某种原因,编译器抛出此错误:
'
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,int>>'不包含'Single' 的定义.
虽然List<T>没有那个方法,但它应该是可见的,因为它是来自System.Linq命名空间的扩展方法(包括在内).显然使用this.Single解决问题,但为什么通过base错误访问?
C#规范第7.6.8节说
当
base.I发生在类或结构中时,I必须表示该类或结构的基类的成员.
这似乎阻止了对扩展方法的访问base.不过它也说
在绑定时,表单的基本访问表达式
base.I和base[E]它们的编写方式完全相同,((B)this).I并且((B)this)[E]在哪里B是构造发生的类或结构的基类.因此,base.I与base[E]对应于 …
我试图IEnumerable<Turtle>在一个派生自已实现的基类的类中实现IEnumerable<Animal>.
为什么base.Cast<Turtle>()在类的任何方法中调用(或基本元素上的任何LINQ方法)都Turtle无法编译?
不可能替换base,this因为它显然导致a StackOverflowException.
以下是复制问题的最小代码示例:
public interface IAnimal {}
public class Animal : IAnimal {}
public class Turtle : Animal {}
public class AnimalEnumerable : IEnumerable<Animal> {
    List<Animal> Animals = new List<Animal>();
    IEnumerator<Animal> IEnumerable<Animal>.GetEnumerator() {
        return Animals.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator() {
        return Animals.GetEnumerator();
    }
}
public class TurtleEnumerable : AnimalEnumerable, IEnumerable<Turtle> {
    IEnumerator<Turtle> IEnumerable<Turtle>.GetEnumerator() {
        return base.Cast<Turtle>().GetEnumerator(); //FAILS WITH "CANNOT RESOLVE SYMBOL Cast"
    }
}
出于某种原因,更换base.Cast<Turtle>().GetEnumerator(); …
考虑这个课程:
public class Thing {
    public string Color { get; set; }
    public bool IsBlue() {
        return this.Color == "Blue";   // redundant "this"
    }
}
我可以省略关键字,this因为它Color是一个属性Thing,我正在编码Thing.
如果我现在创建一个扩展方法:
public static class ThingExtensions {
    public static bool TestForBlue(this Thing t) {
        return t.Color == "Blue";
    }
}
我现在可以将我的IsBlue方法更改为:
public class Thing {
    public string Color { get; set; }
    public bool IsBlue() {
        return this.TestForBlue();   // "this" is now required
    }
} …我有一个类,Deck它包含一个名为的方法Shuffle.
我正在进行重构Deck扩展List<Card>,而不是List<Card> Cards作为一个属性.但是,虽然Cards.OrderBy (a => Guid.NewGuid ())工作,OrderBy (a => Guid.NewGuid ())但不是:
Error CS0103: The name 'OrderBy' does not exist in the current context (CS0103)
为什么这不起作用?
一个类继承自HashSet,以获取一组具有自定义EqualKeys(T x, T y)检查的唯一对象,而不是IEqualityComparer.
public class UniqueSet<T> : HashSet<T> where T : IKey
{
    public new void Add(T item)
    {
         // .. check item for null, empty key etc.
          if (base.Any(t => UniqueSet<T>.EqualKeys(t, item)))
          {
             throw new ArgumentException(..);
          }
          if (!base.Add(item)) throw new ArgumentException(..);
     }
    private static bool EqualKeys(T x, T y) 
    {
       return ((IKey)x).Key.Equals(((IKey)y).Key, StringComparison.CurrentCultureIgnoreCase);
    }
}
代码无法编译,因为我必须替换base.Any为this.Any.
 我恐怕不明白为什么会这样?
我的目标是使用我的功能从 Unity3D 引擎扩展 MonoBehaviour 对象。这就是我所做的:
public static class Extensions {
    public static T GetComponentInChildren<T>(this UnityEngine.MonoBehaviour o, bool includeInactive) {
        T[] components = o.GetComponentsInChildren<T>(includeInactive);
        return components.Length > 0 ? components[0] : default(T);
    }
}
但是当我要使用它时,我只能this在调用前面使用时才能访问它:this.GetComponentInChildren(true)但this应该是隐式的,对吧?
所以我认为我做错了什么......
这是我使用扩展的地方:
public class SomeController : MonoBehaviour {
    private SomeComponent component;
    void Awake() {
        component = this.GetComponentInChildren<SomeComponent>(true);
    }
}
我希望我清楚了我的问题。有没有办法正确扩展 MonoBehaviour(不需要this显式使用关键字)或者为什么会这样?
c# ×7
inheritance ×3
linq ×2
.net ×1
class ×1
hashset ×1
httpresponse ×1
ienumerator ×1
interface ×1