小编asg*_*las的帖子

来自MemberExpression的ReflectedType

鉴于此代码:

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,如果我理解正确的话 - 声明类型.

这是为什么?

.net c# linq-expressions

7
推荐指数
1
解决办法
438
查看次数

使用 webpack-dev-server 时,为什么 Chrome 会在每秒请求之前等待?

有人可以帮助我了解初始连接前数百毫秒内发生的事情吗?

它只发生在每个其他请求上,如图所示。

请求是针对 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:现在似乎是这种情况的摘要,使用时会出现该模式:

  • 通过 webpack-dev-server 从 …

google-chrome-devtools node-http-proxy webpack-dev-server

7
推荐指数
1
解决办法
635
查看次数

在 TypeScript 中获取函数参数名称和类型

我玩过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)。

typescript

7
推荐指数
1
解决办法
1994
查看次数

从Visual Studio 2015.2到2015.3更新后,隐式转换,等于运算符和nullables的组合无法编译

下面的代码在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是枚举时,这似乎只会失败.

c# visual-studio visual-studio-2015

5
推荐指数
1
解决办法
104
查看次数

Typescript和React:使用React.Component和条件类型的props时类型推断失败

在下面的设置中,我尝试为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)

有两点将使推断正确-但在其他方面当然也将是错误的-但可能会提供错误提示:

  1. 我可以删除“道具”类型的这一部分: & (T …

typescript reactjs react-native

5
推荐指数
1
解决办法
90
查看次数

.NET Core 和类型相等

本次测试:

[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)

为什么是这样?我可以做些什么来正确比较类型吗?

.net-core

5
推荐指数
1
解决办法
109
查看次数

Linq to Objects:TakeWhileOrFirst

使用linq将以下内容应用于序列的最可读方式是什么:

TakeWhile elements are valid but always at least the first element
Run Code Online (Sandbox Code Playgroud)

编辑:我更新了标题,更准确.我很抱歉有任何困惑,下面的答案肯定教会了我一些东西!

预期的行为是:Take while元素有效.如果结果是空序列,则无论如何都要取第一个元素.

c# linq-to-objects

4
推荐指数
1
解决办法
150
查看次数

运行NHibernate的单元测试

我有最新的NHibernate主干,我保持我的构建版本在我的项目中使用,一切都很好.但是,NHibernate有我想要解决的错误,或者至少提供工作单元测试以包含在源代码中.我的问题是我根本无法运行NHibernate单元测试,而且我不知道从哪里开始.我已经在官方董事会上发布了与我的问题有关的几十个问题,但尚未收到回复.有谁知道我怎么能让这些东西工作?

nhibernate automated-tests unit-testing

4
推荐指数
1
解决办法
502
查看次数

Shell.Exec在FAKE中

我在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无法访问吗?

f# f#-fake

4
推荐指数
1
解决办法
2210
查看次数

GetInterfaces()返回FullName = null的泛型接口类型

任何人都可以向我解释为什么下面的代码中的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)

请赐教:)

.net c# generics reflection

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

打印时隐藏PDF格式的文本

是否可以在PDF(更具体地说是链接)中写入一些文本,这些文本在发送到打印机时不会打印,而只能在屏幕阅读器中显示?

如果可能,那么任何指向PDF编写.NET库的指针都可能非常受欢迎.

pdf pdf-generation

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

TypeDescriptor.GetProperties(thisType)返回属性,这些属性是只写的

我正在尝试从类型中获取所有属性,但是使用TypeDescriptor.GetProperties(thisType)只会为我提供属性,它具有setter和getter.我有只写属性.有没有办法检索PropertyDescriptorCollection,包括那些?

/导演Asger

c# propertydescriptor typedescriptor

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

在linq查询中筛选所选的可枚举项

是否可以将下面的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# linq-to-objects

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