msdn定义了此方法:返回大于或等于指定的双精度浮点数的最小整数.
但事实上,确实如此
public static double Ceiling (
double a
)
Run Code Online (Sandbox Code Playgroud)
为什么不直接返回int?微软怎么想?
该RuntimeHelpers.GetHashCode(object)方法允许基于对象的身份生成哈希码.MSDN 声明:
RuntimeHelpers.GetHashCode方法始终非虚拟地调用Object.GetHashCode方法,即使对象的类型已重写Object.GetHashCode方法.
[MethodImpl(MethodImplOptions.InternalCall)]
[SecuritySafeCritical]
public static extern int GetHashCode(object o);
Run Code Online (Sandbox Code Playgroud)
但是,在Object.GetHashCode()使用Reflector(.NET 4.0)检查方法时,我们将看到以下代码:
public virtual int GetHashCode()
{
return RuntimeHelpers.GetHashCode(this);
}
Run Code Online (Sandbox Code Playgroud)
这让我相信MSDN文档是错误的,因为Object.GetHashCode从内部调用RuntimeHelpers.GetHashCode(object)会导致堆栈溢出.
那么它的实际行为是什么RuntimeHelpers.GetHashCode(object)以及它是如何工作的?它是如何计算哈希值的?
System.Type包含一个UnderlyingSystemType属性.Msdn 表示:
指示表示此类型的公共语言运行库提供的类型.
在大多数情况下,此属性只返回当前Type实例.
我的问题是,在什么情况下这个属性不会返回当前Type实例本身.在这些情况下,当前Type实例和返回的底层系统类型将是什么类型?
我正在尝试使用Microsoft.Bcl.Async和代码分析,但是当我运行代码分析时,我得到一个或多个错误.
我正在使用Visual Studio 2012和Update 2.
这对我来说很容易重现:
.Net 4.References然后选择Manage NuGet Packages...Online并async在Search Online框中键入.Async for .Net Framework 4 ....点击Install并接受所有问题.Main()一行说:TaskEx.Delay(1000);和ausing System.Threading.Tasks;Enable Code Analysis on Build.我收到两个代码分析错误:
CA0052运行代码分析时出错CA0052:未选择任何目标.[错误和警告](全球)
CA0055运行代码分析CA0055时出错:无法加载ConsoleApplication2.exe.读取模块"ConsoleApplication2"时遇到以下错误:无法解析成员引用:[Microsoft.Threading.Tasks,Version = 1.0.12.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a] System.Threading.Tasks.TaskEx :: Delay.[错误和警告](全球)
我为其他测试程序得到了不同的代码分析错误.我试过的基本Windows窗体应用程序给了我:
CA0001错误运行代码分析CA0001:读取模块"AsyncForNet4"时遇到以下错误:无法解析成员引用:[Microsoft.Threading.Tasks,Version = 1.0.12.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a] System.Threading. Tasks.TaskEx ::延迟.[错误和警告](全球)
两个问题:
为了避免在搜索已知类型的属性时使用过时的非泛型语法,通常会使用System.Reflection.CustomAttributeExtensions类中的扩展方法(从.NET 4.5开始).
但是,如果在重写方法的返回参数(或重写的属性/索引器的访问者)上搜索属性,则这似乎会失败.
我正在使用.NET 4.6.1体验这一点.
简单复制(完整):
using System;
using System.Reflection;
namespace ReflectionTrouble
{
class B
{
//[return: MyMark("In base class")] // uncommenting does not help
public virtual int M() => 0;
}
class C : B
{
[return: MyMark("In inheriting class")] // commenting away attribute does not help
public override int M() => -1;
}
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)] // commenting away AttributeUsage does not help
sealed class MyMarkAttribute : Attribute
{ …Run Code Online (Sandbox Code Playgroud) BCL中有几个地方可以使用IEqualityComparer.像Enumerable.Contains或Dictionary Constructor.如果我对默认的不满意,我可以提供我的比较器.
有时我想知道集合是否包含我所引用的那个对象.不是任何其他意义上被认为"相等"的那个.
问题是:BCL中是否存在仅依赖于ReferenceEquals方法的标准相等比较器?
我自己写的是这样的:
class ReferenceComparer<T> : IEqualityComparer<T> where T : class
{
private static ReferenceComparer<T> m_instance;
public static ReferenceComparer<T> Instance
{
get
{
return m_instance ?? (m_instance = new ReferenceComparer<T>());
}
}
public bool Equals(T x, T y)
{
return ReferenceEquals(x, y);
}
public int GetHashCode(T obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}
Run Code Online (Sandbox Code Playgroud)
我没有测试它彻底,也没有考虑很多的场景,但它似乎使Enumerable.Contains和Dictionary很高兴.
我使用Reflector来查看String.Format的实现,并且一直认为带有1,2和3参数的String.Format的重载是采用对象数组的方法的优化版本.但是,我发现内部它们创建了一个对象数组,然后调用一个带有对象数组的方法.
1 arg
public static string Format(string format, object arg0)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0 });
}
Run Code Online (Sandbox Code Playgroud)
2 args
public static string Format(string format, object arg0, object arg1)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0, arg1 });
}
Run Code Online (Sandbox Code Playgroud)
3 args
public static string Format(string format, object arg0, object arg1, object arg2)
{
if (format == null)
{
throw …Run Code Online (Sandbox Code Playgroud) 如果上面的陈述是正确的,那么为什么当我在.Net BCL上使用反射器时,我看到它被大量使用了?
编辑:让我重新说一下:我在人类或编译器编写的反射器中看到的所有GO-TO是什么?
在我看来,.NET存在极端缺乏安全,不可变的集合类型,特别是BCL,但我还没有看到很多工作在外面完成.有没有人有任何指向(优选)生产质量,快速,不可变的.NET集合库.快速列表类型是必不可少的.我还没准备好切换到F#.
*编辑:注意搜索者,很快就会进入BCL: .NET不可变集合
.NET Framework 4.5开发人员预览版中的新增内容
能够自定义反射上下文以通过CustomReflectionContext类覆盖默认反射行为.
这是什么目的ReflectionContext?MSDN在这个问题上并不十分清楚.
.net ×8
c# ×8
clr ×2
reflection ×2
.net-4.5 ×1
comparison ×1
frameworks ×1
fxcop ×1
goto ×1
immutability ×1