相关疑难解决方法(0)

如何使用错误消息或异常返回NotFound()IHttpActionResult?

IHttpActionResult当我的WebApi GET操作中找不到某些内容时,我将返回NotFound .除了这个响应,我想发送自定义消息和/或异常消息(如果有的话).当前ApiControllerNotFound()方法不提供传递消息的重载.

有没有办法做到这一点?或者我将不得不写自己的习惯IHttpActionResult

c# httpresponse http-status-code-404 asp.net-web-api

92
推荐指数
5
解决办法
7万
查看次数

为什么我不能从扩展类型的基类调用扩展方法?

我正在尝试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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,编译器抛出此错误:

' 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.Ibase[E]它们的编写方式完全相同,((B)this).I并且((B)this)[E]在哪里B是构造发生的类或结构的基类.因此,base.Ibase[E]对应于 …

c# linq inheritance extension-methods

17
推荐指数
1
解决办法
5723
查看次数

不能在派生类的IEnumerable基类上使用LINQ方法

我试图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"
    }
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,更换base.Cast<Turtle>().GetEnumerator(); …

c# ienumerator inheritance interface

15
推荐指数
2
解决办法
1942
查看次数

在他们扩展的类中使用扩展方法

考虑这个课程:

public class Thing {

    public string Color { get; set; }

    public bool IsBlue() {
        return this.Color == "Blue";   // redundant "this"
    }

}
Run Code Online (Sandbox Code Playgroud)

我可以省略关键字,this因为它Color是一个属性Thing,我正在编码Thing.

如果我现在创建一个扩展方法:

public static class ThingExtensions {

    public static bool TestForBlue(this Thing t) {
        return t.Color == "Blue";
    }

}
Run Code Online (Sandbox Code Playgroud)

我现在可以将我的IsBlue方法更改为:

public class Thing {

    public string Color { get; set; }

    public bool IsBlue() {
        return this.TestForBlue();   // "this" is now required
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# extension-methods class

3
推荐指数
1
解决办法
2620
查看次数

为什么我不能在扩展List的类中调用OrderBy?

我有一个类,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)

为什么这不起作用?

.net c# linq inheritance extension-methods

2
推荐指数
1
解决办法
3871
查看次数

使用base.Any(..)警告:'HashSet'不包含'Any'的定义

一个类继承自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);
    }
}
Run Code Online (Sandbox Code Playgroud)

代码无法编译,因为我必须替换base.Anythis.Any.
我恐怕不明白为什么会这样?

c# hashset

2
推荐指数
1
解决办法
692
查看次数

扩展 MonoBehaviour 的扩展方法

我的目标是使用我的功能从 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我要使用它时,我只能this在调用前面使用时才能访问它:this.GetComponentInChildren(true)this应该是隐式的,对吧?

所以我认为我做错了什么......

这是我使用扩展的地方:

public class SomeController : MonoBehaviour {

    private SomeComponent component;

    void Awake() {
        component = this.GetComponentInChildren<SomeComponent>(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望我清楚了我的问题。有没有办法正确扩展 MonoBehaviour(不需要this显式使用关键字)或者为什么会这样?

c# extension-methods unity-game-engine

2
推荐指数
1
解决办法
5251
查看次数