出于某种原因,C#不允许在这样的泛型类中使用==运算符:
class Mine<T> where T : struct
{
T val;
public T Value
{
set
{
if (val == value) // Operator '==' cannot be applied to operands of type T and T happens here
{
// .. do something ...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我用val.Equals(value)替换==我有代码按预期工作但如果我看字节码它看起来要复杂得多.使用==和Equals()比较循环中的int变量的一个非常简单的测试表明,Equals()版本比"=="版本慢两倍.
我想知道是否有一种方法来比较泛型类中的原始值类型,这些类型与==运算符一样快.欢迎任何想法.
编辑: 我在计时器之间迷路了.性能差异并不那么显着.这是我最新的结果:
== operator 1974380 ticks
Equals() 1976358 ticks
== operator in another static function 1974604 ticks
EqualityComparer<int>.Default... 32486695 ticks
Run Code Online (Sandbox Code Playgroud)
简而言之:Equals()足够好了.
我正在寻找一种在 C# 中记录操作参数的方法。代码如下所示:
/// <summary>
/// Do some action with parameters.
/// </summary>
/// <params name="someAction"> This is action but what about parameters? </param>
public void Do(Action<int,int,string> someAction)
Run Code Online (Sandbox Code Playgroud)
如何命名和记录“someAction”的参数,以便它们出现在智能感知中?
我想用声明的参数名生成泛型类的名称.例如,如果我有通用类和实例化的类,如下所示我想打印"MyClass<P1,M>".
class MyClass<P1, M1> {}
// ... some code removed here
var myInstance = new MyClass<int,string>();
Run Code Online (Sandbox Code Playgroud)
现在我得到我的类型信息myInstance,然后是通用类型定义,如下所示:
// MyClass<int,string> type info here
var type = myInstance.GetType();
// MyClass<P1, M1> type info here
var genericType = type.GetGenericTypeDefinition();
Run Code Online (Sandbox Code Playgroud)
调试器显示genericType具有所有参数名称和类型信息的属性GenericTypeParameters.但是,我无法从我的C#代码访问该集合,并且转换genericType为System.RuntimeType类不起作用,因为RuntimeType是内部的.
那么有没有办法以某种方式访问GenericTypeParameters属性或我在这里SOL?环境VS2015,.NET 4.6.1
我想描述返回未知/任何类型的 JSON 对象的 OpenAPI。
如果我在下面的 yaml 中定义返回类型,我仍然会看到生成的客户端仅返回原始字符串。
responses:
200:
description: Returns any JSON object
schema:
type: string
format: object
Run Code Online (Sandbox Code Playgroud)
有没有办法将返回类型描述为 JSON 对象而不描述其架构?
我有简单的Nancy自托管C#项目,它在NancyModule中反序列化数据,如下所示:
Post["/build"] = (something) => { var data = this.Bind<Brick>(); }
Run Code Online (Sandbox Code Playgroud)
我收到"已超出最大JSON输入长度".当Request.Body.Length接近2MB时.我希望将来发送数十兆字节的数据,而2MB则太低了.我可以删除此限制吗?
我正在寻找一种简单的方法来获取从类开始的方法的反射信息,并一直返回到声明接口。下面是一段简化的代码:
public interface Ix
{
void Function();
}
public class X : Ix
{
public void Function() {}
}
public class Y : X
{
}
Run Code Online (Sandbox Code Playgroud)
类 X 的方法信息没有关于在 Ix 接口中声明的函数的信息。这是电话:
var info = typeof(X).GetMethod("Function");
var baseInfo = info.GetBaseDefinition()
Run Code Online (Sandbox Code Playgroud)
它返回以下数据:
info.DeclaringType --> MyNamespace.X
info.ReflectedType --> MyNamespace.X
baseInfo.DeclaringType --> MyNamespace.X
baseInfo.ReflectedType --> MyNamespace.X
Run Code Online (Sandbox Code Playgroud)
为类 Y 返回相同的信息。
我如何确定这个函数是在接口 Ix 中声明的,而无需遍历所有已实现的接口和类 X 或 Y 的基类?我可能会遗漏一些简单的东西,但我无法弄清楚是什么。这可能是一个错误吗?
这是 .Net Core SDK 版本 2.1.104 和 Visual Studio 2017
我知道这个问题已被多次回答,而且除了一个以外,我所用的解决方案几乎适用于所有情况.
有问题的代码是这样的:
Path.GetFullPath(Path.Combine(rootFolder, relativeFilePath))
Run Code Online (Sandbox Code Playgroud)
即使使用凌乱的相对路径,UNC和绝对路径,它在大多数情况下都能正常工作,如下所示:
// These results are OK
Path.GetFullPath(Path.Combine(@"a:\b\c", @"e.txt")); // Returns: "a:\b\c\e.txt"
Path.GetFullPath(Path.Combine(@"a:\b\c", @"..\e.txt")); // Returns: "a:\b\e.txt"
Path.GetFullPath(Path.Combine(@"a:\b\c", @"d:\e.txt")); // Returns: "d:\e.txt"
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,它不能按预期工作:
Path.GetFullPath(Path.Combine(@"a:\b\c", @"\e.txt"))
Run Code Online (Sandbox Code Playgroud)
预期的结果是"a:\ e.txt",但代码返回"c:\ e.txt"."c:"是当前的驱动器所以它解决了拼图的一部分,但为什么会发生这种情况?有没有其他方法可以获得适用于所有情况的完整路径?
编辑:根据答案的信息,这里是有效的解决方案.可能需要一些空检查和替代目录分隔符char检查:
var rootDir = @"a:\b\c";
var filePath = @"\e.txt";
var result = (Path.IsPathRooted(filePath) &&
Path.GetPathRoot(filePath) == Path.DirectorySeparatorChar.ToString()) ?
Path.GetFullPath(Path.Combine(Path.GetPathRoot(rootDir), filePath.Substring(1))) :
Path.GetFullPath(Path.Combine(rootDir, filePath));
Run Code Online (Sandbox Code Playgroud)