鉴于此代码:
static void Main(string[] args)
{
Expression<Func<SomeDerivedClass, object>> test = i => i.Prop;
var body = (UnaryExpression) test.Body;
Console.WriteLine(((MemberExpression) body.Operand).Member.ReflectedType);
}
public class SomeClass
{
public int Prop { get; private set; }
}
public class SomeDerivedClass : SomeClass
{
}
Run Code Online (Sandbox Code Playgroud)
我希望ReflectedType是SomeDerivedClass,因为它是表达式的参数类型.但它是SomeClass,如果我理解正确的话 - 声明类型.
这是为什么?
有人可以帮助我了解初始连接前数百毫秒内发生的事情吗?
它只发生在每个其他请求上,如图所示。
请求是针对 ASP.NET Core Web 应用程序对 localhost 上的相同资源发出的 POST。
我也看过其他类似的问题和答案,但我没有看到在初始连接之前什么都没有的例子。其他人似乎都有一个“停顿”或“排队”的酒吧。
“短”请求如下所示:
Chrome 版本:74.0.3729.131(官方版本)(64 位)
更新 1:这在 Microsoft Edge 中不会发生。每个请求都很短。不用等。
更新 2:下载一个长请求的 HAR 文件显示一个很长的“连接”时间:
"timings": {
"blocked": 1.135999995024409,
"dns": 0.0030000000000000027,
"ssl": -1,
"connect": 301.202,
"send": 0.18900000000002137,
"wait": 79.29900000206612,
"receive": 3.750999996555038,
"_blocked_queueing": 0.8449999950244091
},
Run Code Online (Sandbox Code Playgroud)
短的连接时间为 -1:
"timings": {
"blocked": 0.9680000060191378,
"dns": -1,
"ssl": -1,
"connect": -1,
"send": 0.091,
"wait": 50.74499999642931,
"receive": 2.582000000984408,
"_blocked_queueing": 0.8130000060191378
},
Run Code Online (Sandbox Code Playgroud)
但为什么?
更新 3:事实证明,这只发生在通过 webpack-dev-server 代理时。我也会添加那个标签。尽管如此,它仍然只发生在 Chrome 中。
更新 4:现在似乎是这种情况的摘要,使用时会出现该模式:
我玩过Parameters<T>TypeScript 中的类型,它返回一个带有参数类型的元组。现在我想知道是否有一种方法不仅可以返回参数的类型,还可以返回名称?
就像是:
function MyFunc(a: string, b: boolean, ...args: string[]) {}
type extractedType = ParametersWithName<typeof MyFunc>
Run Code Online (Sandbox Code Playgroud)
当extractedType结果最终为:
{
a: string,
b: string
}
Run Code Online (Sandbox Code Playgroud)
我会用它来从常规函数参数转换为接收参数对象的函数(如 React props)。
下面的代码在VS2015.2中编译,但升级到VS2015.3后失败了error CS0019: Operator '==' cannot be applied to operands of type 'Registration<Something>' and 'Something'.
public class Class1
{
public Class1()
{
var a = new Registration<Something>();
var x = a == Something.Bad; // this line fails in VS2015.3
}
}
public struct Registration<T> where T:struct
{
public static implicit operator T?(Registration<T> registration)
{
return null;
}
}
public enum Something
{
Good,
Bad
}
Run Code Online (Sandbox Code Playgroud)
我无法在更新3的更改日志中找到关于此类更改的任何通知.有人可以告诉我,为什么会发生这种情况?哪个是正确的行为?
编辑:隐式转换,等式运算符和nullables ...和枚举的组合.当T是枚举时,这似乎只会失败.
在下面的设置中,我尝试为onChange函数中的value参数获取正确的推断类型。下面的设置为我提供了一个类型值,string | number尽管我本以为应该string为该值分配一个string。
import * as React from 'react'
type NumberProps = {
decimals: number;
};
type StringProps = {};
type Props<T> = {
value: T;
onChange: (value: T) => void;
} & (T extends number ? NumberProps : StringProps);
class Y<T extends string | number> extends React.Component<Props<T>> {}
new Y({ value: 2.5, onChange:((value) => { /* value is inferred as string | number, but should be number */ })})
Run Code Online (Sandbox Code Playgroud)
有两点将使推断正确-但在其他方面当然也将是错误的-但可能会提供错误提示:
我可以删除“道具”类型的这一部分: & (T …
本次测试:
[Fact]
public void X()
{
Assert.IsType<Expression<Func<int>>>(Expression.Lambda<Func<int>>(Expression.Constant(1)));
}
Run Code Online (Sandbox Code Playgroud)
在 .NET Framework 上运行,但在 .NET Core(在我的情况下为 3.1)上失败并出现以下错误:
Expected: System.Linq.Expressions.Expression`1[[System.Func`1[[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
Actual: System.Linq.Expressions.Expression0`1[[System.Func`1[[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我可以做些什么来正确比较类型吗?
使用linq将以下内容应用于序列的最可读方式是什么:
TakeWhile elements are valid but always at least the first element
Run Code Online (Sandbox Code Playgroud)
编辑:我更新了标题,更准确.我很抱歉有任何困惑,下面的答案肯定教会了我一些东西!
预期的行为是:Take while元素有效.如果结果是空序列,则无论如何都要取第一个元素.
我有最新的NHibernate主干,我保持我的构建版本在我的项目中使用,一切都很好.但是,NHibernate有我想要解决的错误,或者至少提供工作单元测试以包含在源代码中.我的问题是我根本无法运行NHibernate单元测试,而且我不知道从哪里开始.我已经在官方董事会上发布了与我的问题有关的几十个问题,但尚未收到回复.有谁知道我怎么能让这些东西工作?
我在Exec这里找到了这个函数http://fsharp.github.io/FAKE/apidocs/fake-processhelper-shell.html.
Target "UpdateTools" (fun _ ->
Exec "cmd"
)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行它时,我不断收到此错误:"未定义值或构造函数'Exec'".
我是FAKE的新手并没有使用过F#,所以请原谅我这应该是显而易见的.
有人能告诉我为什么这样的api无法访问吗?
任何人都可以向我解释为什么下面的代码中的GetInterfaces()返回一个FullName = null的接口类型?
public class Program
{
static void Main(string[] args)
{
Type[] interfaces = typeof (Data<>).GetInterfaces();
foreach (Type @interface in interfaces)
{
Console.WriteLine("Name='{0}' FullName='{1}'", @interface.Name, @interface.FullName ?? "null");
}
}
}
public class Data<T> : IData<T>
{
public T Content { get; set; }
}
public interface IData<T>
{
T Content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
该计划的输出是:
Name=IData`1' FullName='null'
Run Code Online (Sandbox Code Playgroud)
我有点期待:
Name=IData`1'
FullName='ConsoleApplication2.IData`1'
Run Code Online (Sandbox Code Playgroud)
请赐教:)
是否可以在PDF(更具体地说是链接)中写入一些文本,这些文本在发送到打印机时不会打印,而只能在屏幕阅读器中显示?
如果可能,那么任何指向PDF编写.NET库的指针都可能非常受欢迎.
我正在尝试从类型中获取所有属性,但是使用TypeDescriptor.GetProperties(thisType)只会为我提供属性,它具有setter和getter.我有只写属性.有没有办法检索PropertyDescriptorCollection,包括那些?
/导演Asger
是否可以将下面的linq查询合并到一个查询中?
var checkBoxes = from x in FindAll<CheckBox>()
where x.Checked
select new
{
RecordType = Type.GetType(x.Attributes["RecordType"]),
RecordId = Int32.Parse(x.Attributes["RecordId"])
};
checkBoxes = from x in checkBoxes
where x.RecordType != typeof(DomainModel.Group)
select x;
Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×2
typescript ×2
.net-core ×1
f# ×1
f#-fake ×1
generics ×1
nhibernate ×1
pdf ×1
react-native ×1
reactjs ×1
reflection ×1
unit-testing ×1