我遇到了一个有趣的情况,我NRE从Uri.TryCreate方法中得到它应该返回false.
您可以重现以下问题:
Uri url;
if (Uri.TryCreate("http:Ç", UriKind.RelativeOrAbsolute, out url))
{
Console.WriteLine("success");
}
Run Code Online (Sandbox Code Playgroud)
我猜它在解析期间失败了,但是当我尝试"http:A"例子时,它返回true并将其解析为相对url.即使解析失败也应该false按照我的理解返回,这可能是什么问题?这似乎是实现中的错误原因文档没有提到有关此方法的任何异常.
该错误发生在.NET 4.6.1中,但不是4.0
我有List<T>一些包含一些数据.我想把它传递给一个接受的函数ReadOnlySpan<T>.
List<T> items = GetListOfItems();
// ...
void Consume<T>(ReadOnlySpan<T> buffer)
// ...
Consume(items??);
Run Code Online (Sandbox Code Playgroud)
在这个特定的例子中,T是,byte但它并不重要.
我知道我可以.ToArray()在List上使用,并构造一个span,例如
Consume(new ReadOnlySpan<T>(items.ToArray()));
Run Code Online (Sandbox Code Playgroud)
然而,这会创建(看似)不必要的项目副本.有没有办法直接从列表中获取Span?List<T>是在T[]幕后实施的,所以从理论上来说这是可能的,但在实践中并没有我能看到的那么多?
当我在查看String.Join方法实现时,我看到了一个这样的for循环:
public static string Join(string separator, params object[] values)
{
...
for (int index = 1; index < values.Length; ++index)
{
sb.Append(separator);
if (values[index] != null) // first if statement
{
string str2 = values[index].ToString();
if (str2 != null) // second if statement
sb.Append(str2);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
在这里,第二个if语句对我来说似乎是多余的.我认为如果values[index] != null是真的那么怎么可能是values[index].ToString() == null 真的?据我所知,ToString总是必须归还一些东西,对吧?即使类型没有覆盖ToString方法,它也应该返回Type的完全限定名称(Namespace + Class name).所以当我在.NET Framework源代码中看到它时,我想也许有一个原因,我错过了什么.如果有一个原因,这是什么?
当我试图定义一个继承自System.ValueType或System.Enum类的类时,我收到一个错误:
Cannot derive from special class System.ValueType
Run Code Online (Sandbox Code Playgroud)
我理解这个错误,但是我无法理解的是什么让ValueType课程变得特别?我的意思是没有关键字(like sealed)或属性来指定不能继承此类.ValueType有两个属性,Serializable并ComVisible而是与case.The他们都不是相关文件说:
尽管ValueType是值类型的隐式基类,但您无法创建直接从ValueType继承的类.相反,单个编译器提供语言关键字或构造(例如C#中的struct和Visual Basic中的Structure ... End Structure)以支持值类型的创建.
但它没有回答我的问题.所以我的问题是在这种情况下如何通知编译器?编译器是否直接检查该类是否ValueType或Enum当我尝试创建从类继承的类时?
编辑:所有结构implicitly都继承自ValueType,但是明确地继承了Enum类,那么它是如何工作的?编译器如何弄清楚这种情况,所有这些都是由编译器硬编码的?ValueType
如果我有这样的代码:
public class Program
{
public static void Main()
{
string bar = "";
int foo = 24;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以Main使用以下方法获取声明的局部变量:
var flag = BindingFlags.Static | BindingFlags.Public;
var fields = typeof(Program).GetMethod("Main", flags).GetMethodBody().LocalVariables;
Run Code Online (Sandbox Code Playgroud)
这将返回一个IList<LocalVariableInfo>与LocalVariableInfo只有三个属性:IsPinned,LocalIndex和LocalType.所以没有Name财产存在.
我想知道的是你可以看到生成的变量名称IL code:
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 1
.locals init ([0] string bar,
[1] int32 foo)
IL_0000: nop
IL_0001: ldstr …Run Code Online (Sandbox Code Playgroud) 可以将字符串的模板存储在变量中并使用插值吗?
var name = "Joe";
var template = "Hi {name}";
Run Code Online (Sandbox Code Playgroud)
然后我想做一些事情:
var result = $template;
Run Code Online (Sandbox Code Playgroud)
原因是我的模板将来自数据库.
当我尝试从我们的 git 服务器中拉取时,我收到此错误:
致命:无法访问“xxx”:OpenSSL SSL_connect:连接到 xxx 的 SSL_ERROR_SYSCALL
当发生这种情况之前,我可以通过简单地恢复系统来解决它,但是这次我的系统还原点由于某种原因被删除了,我也不能这样做。
所以发生这种情况是因为我的系统设置中与 SSL 相关的某些内容发生了变化,但我不知道为什么。
我尝试安装 git 以使用 Windows 证书。store 而不是 OpenSSL,我收到了这个错误:
致命:无法访问“xxx”:schannel:无法接收握手,SSL/TLS 连接失败
同样的问题,不同的错误信息。服务器没有在客户端问候之后发回问候消息。我认为这可能会发生,因为服务器不支持我在客户端 hello 消息中向服务器发送的密码套件。因此,我尝试配置组策略并将服务器使用的密码套件按顺序排列。但这没有任何区别。
我能够通过浏览器连接 git 服务器的站点。所以我的问题是,我能做些什么来解决这个问题?
这是我今天看到的一个奇怪的情况:
我有一个通用列表,我希望添加项目到我的列表与它的索引器像这样:
List<string> myList = new List<string>(10);
myList[0] = "bla bla bla...";
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我得到了 ArgumentOutOfRangeException

然后我看了List<T>索引器集方法,这里是:
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries"), __DynamicallyInvokable]
set
{
if ((uint) index >= (uint) this._size)
ThrowHelper.ThrowArgumentOutOfRangeException(); //here is exception
this._items[index] = value;
++this._version;
}
Run Code Online (Sandbox Code Playgroud)
还看了一下Add方法:
[__DynamicallyInvokable]
public void Add(T item)
{
if (this._size == this._items.Length)
this.EnsureCapacity(this._size + 1);
this._items[this._size++] = item;
++this._version;
}
Run Code Online (Sandbox Code Playgroud)
现在,我看到两种方法都使用相同的方式:
// Add() Method
this._items[this._size++] = item;
// Setter method
this._items[index] = value;
Run Code Online (Sandbox Code Playgroud)
这_items是一个类型的数组 …
精简版 :
我们可以Func<T,T>使用以下类型:
typeof(Func<,>)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想获得类型Func<T, bool>,我应该使用什么,或者可以做什么呢?显然,这不编译:
typeof(Func<, bool>)
Run Code Online (Sandbox Code Playgroud)
长版:
考虑以下场景,我有两个类似的方法,我想Func<T, int>使用Reflection 得到第二个():
public void Foo<T>(Func<T, bool> func) { }
public void Foo<T>(Func<T, int> func) { }
Run Code Online (Sandbox Code Playgroud)
我正在尝试这个:
var methodFoo = typeof (Program)
.GetMethods()
.FirstOrDefault(m => m.Name == "Foo" &&
m.GetParameters()[0]
.ParameterType
.GetGenericTypeDefinition() == typeof (Func<,>));
Run Code Online (Sandbox Code Playgroud)
但是,由于的泛型类型定义Func<T, bool>和Func<T, int>相等,它给我的第一个方法.要解决这个问题,我可以执行以下操作:
var methodFoo = typeof (Program)
.GetMethods()
.FirstOrDefault(m => m.Name == "Foo" &&
m.GetParameters()[0]
.ParameterType
.GetGenericArguments()[1] == typeof(int));
Run Code Online (Sandbox Code Playgroud)
然后我得到了正确的方法,但我不喜欢这种方式.对于更复杂的情况,这似乎是一种开销.我想要做的是Func<T,bool>在上面的失败尝试中得到类似的类型,然后我可以使用这个重载而不是使用Linq …
我发现了IL code一个简单的程序:
long x = 0;
for(long i = 0;i< int.MaxValue * 2L; i++)
{
x = i;
}
Console.WriteLine(x);
Run Code Online (Sandbox Code Playgroud)
我在发布模式下构建此代码并IL code生成:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 28 (0x1c)
.maxstack 2
.locals init ([0] int64 x,
[1] int64 i)
IL_0000: ldc.i4.0
IL_0001: conv.i8
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: conv.i8
IL_0005: stloc.1
IL_0006: br.s IL_000f
IL_0008: ldloc.1
IL_0009: stloc.0
IL_000a: ldloc.1
IL_000b: ldc.i4.1
IL_000c: conv.i8
IL_000d: …Run Code Online (Sandbox Code Playgroud)