小编jas*_*per的帖子

接口上的C#泛型隐式强制转换失败

下面为什么不编译?什么是关于导致编译器认为它无法Container<T>转换为接口的接口T,何时T是接口?我不认为这是一个协变问题,因为我不是在倾斜,但也许是.这很像为什么C#编译器不调用隐式转换运算符?但我不认为它是完全一样的.

Product pIn =null;
Product pOut;
Container<Product> pContainer;

List<Product> pListIn = null;
List<Product> pListOut;
Container<List<Product>> pListContainer;

IList<Product> pIListIn = null;
IList<Product> pIListOut;
Container<IList<Product>> pIListContainer;

pContainer = pIn;
pOut = pContainer; // all good

pListContainer = pListIn; 
pListOut = pListContainer; // all good too

pIListContainer = pIListIn; // fails , cant do implicit cast for some reason
pIListOut = pIListContainer; // and here too
Run Code Online (Sandbox Code Playgroud)
class Container<T>
{
 private T value;

 private Container(T item) …
Run Code Online (Sandbox Code Playgroud)

c# generics casting c#-4.0

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

是否有.net的序列化器将输出c#代码?

我正在寻找一个可以获取实例的序列化程序,并将其序列化为一个包含c#代码的字符串,表示图形的内容.该类的功能类似于SerializeObjectJSON.NET.

我知道只有一组非常狭窄的结构可以使用,但我感兴趣的结构非常简单,他们会这样做.

如果有人知道具有类似功能的Visual Studio Visualizer,则可获得奖励积分.

编辑: 输出将在编译时在不同的应用程序中使用.我不需要在运行时反序列化输出(c#代码),它会保存到文件中进行分析.

var foo = new Foo() { Number = 1, Bar = new Bar() { Str = "Bar"}};
string sourceCode = Magic.SerializeObject(foo);
Run Code Online (Sandbox Code Playgroud)

输出:

Foo obj = new Foo();
obj.Number = 1;
obj.RefType = null; // infer this
obj.Bar = new Bar();
obj.Bar.Str = "Bar";
Run Code Online (Sandbox Code Playgroud)

.net c# serializer

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

使用泛型在编译时强制执行有效的结构

通过滥用c#中的类型系统,我可以创建代码,编译器将强制执行规则,以确保不执行不可能的操作.在下面的代码中,它特定于矩阵乘法.

显然,下面的内容完全不切实际/错误,但有没有理由我们将来在c#中不能有这样的东西,我可以在其中定义类似Matrix <2,2>的类型并让编译器确保安全性?

此外,在任何主流语言中都存在这样的事情吗?我怀疑C++中的元编程可能会有这样的事情吗?

public abstract class MatrixDimension { }

public class One : MatrixDimension { }
public class Two : MatrixDimension { }
public class Three : MatrixDimension { }

public class Matrix<TRow, TCol>
    where TRow : MatrixDimension
    where TCol : MatrixDimension
{

// matrix mult. rule. N×M * M×P  = N×P 
    public Matrix<TRow, T_RHSCol> Mult<T_RHSCol>(Matrix<TCol, T_RHSCol> rhs)
        where T_RHSCol : MatrixDimension
    { return null;}
}

public class TwoByTwo : Matrix<Two, Two> { }

public void Main()
{
    var …
Run Code Online (Sandbox Code Playgroud)

c# generics types

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

代码合同重写失败 - libpaths命令

我的解决方案/项目构建良好,关闭此特定项目的代码合同.

当我打开我的项目执行运行时合同检查时,构建失败.CC重写者开始按照以下方式提出数百条消息:

Could not resolve member reference: ....

看来这个问题是由通过libpaths参数传递给ccrewrite的项目引用的顺序引起的.当我拆开构建并在命令行上运行它时,我按照特定顺序手动指定引用到libpaths,ccrewrite工作,构建完成.

例如

ccrewrite /level:4 /rewrite "/assemblyMode=standard" /throwonfailure /libpaths:"X Y Z" "target.dll" -- fails :(

ccrewrite /level:4 /rewrite "/assemblyMode=standard" /throwonfailure /libpaths:"Z Y X" "target.dll" -- compiles/rewrites :)

这对我来说有点奇怪,我以为ccrewrite应该能够处理任意顺序,特别是因为提供的'破坏'排序是msbuild传递给Microsoft.CodeContracts.targets文件/ ccrewrite,但是这对CC人来说是一个问题.我也在3台不同的机器上进行了测试,结果相同,所以它要么是ccrewrite问题,要么是ccrewrite不喜欢的特定csproj的奇怪之处.

我想知道的是,我可以确保我的参考项目的特定顺序,它们被链接传递到CC目标文件吗?我原以为构建引擎会在ItemGroup//ProjectReference元素下使用它在项目csproj文件中读取的顺序,但事实并非如此.

有任何想法吗?

.net msbuild code-contracts ccrewrite

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

WPF RichTextBox文档创建线程问题

关于线程,我在RTB和文档生成方面遇到了一些问题.

当TextChanged事件在RTB上触发时,会创建一个新的thead,并将文档生成卸载到此.这可能需要几秒钟,阻塞调用,所以它确实需要在另一个线程上以保持UI响应.

当我尝试将新生成的文档添加到DocumentRTB 的属性时,我遇到的问题是一个例外.(调用线程无法访问此对象,因为另一个线程拥有它.)这不是因为忘记使用Dispatcher.Invoke,因为生成异常的那个,而是因为我在其他线程上创建FlowDocument/Paragraph/Run实例比UI线程(我想??).

有没有办法实现我在这里寻找的东西?

更新

    private void rtbQuery_TextChanged(object sender, TextChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Requires update; on thread:" + System.Threading.Thread.CurrentThread.ManagedThreadId);

        backgroundWorker.RunWorkerAsync();
    }

    private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Generating; on thread:" + System.Threading.Thread.CurrentThread.ManagedThreadId);

        DocumentGenerator dgen = new DocumentGenerator();
        string queryText = getQueryText();

        e.Result = dgen.GenerateDocument(queryText);
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Assigning; on thread:" + System.Threading.Thread.CurrentThread.ManagedThreadId);

        FlowDocument doc = (FlowDocument)e.Result;
        txtQuery.Document = doc; // ! The calling thread cannot access this object …
Run Code Online (Sandbox Code Playgroud)

.net wpf multithreading richtextbox

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

.NET JIT编译器会优化方法调用吗?

我从ASP.NET应用程序返回的堆栈跟踪,从ArgumentNullException生成,给人的印象是下面代码的最后一行发生错误.据我所知,这是不可能的,但如果JIT优化了调用Bar,这将导致不同的堆栈跟踪,它将解释所有.我肯定知道它不是c#编译器,因为CIL看起来像我期望的那样.是否有可能JIT编译器删除了对Bar的调用?

c#4,.NET 4.0.30319.1,ASP.NET 4.0.30319.1

编辑: 我应该提到这是一个发布配置,优化代码= on,Debug Info = pdb-only.

Stack Trace:

[ArgumentNullException: Value cannot be null. Parameter name: value]
CreateHiddenField(HtmlTextWriter tr, String name, String value) in Foo.cs:129
Foo(IHttpContext context, HtmlTextWriter writer) in Foo.cs:106
Run Code Online (Sandbox Code Playgroud)
private static void Foo(IHttpContext context, HtmlTextWriter writer)
{ // line 103
  Bar(writer, AuthorizationServerResponseDetailsHttpRequestParser.RequestSAMLFieldName, context);
  Bar(writer, AuthorizationServerResponseDetailsHttpRequestParser.RequestTargetFieldName, context);
  // line 106 - blank line in source code.
  CreateHiddenField(tr, name, string.Empty); // looks like its here
}

private static void Bar(HtmlTextWriter tr,string name, IHttpContext context)
{ …
Run Code Online (Sandbox Code Playgroud)

.net asp.net clr c#-4.0

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

对于chrome devtools中的JS对象,是否存在DebuggerDisplay的等效项?

.net框架具有非常有用的调试器显示属性,允许您指定类或字段在调试器变量窗口中的显示方式.

是否有相当于这个(可以在chrome dev工具中使用),我可以在我的JS对象上使用它?

javascript google-chrome-devtools

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

ReferenceEquals - true和Equals - false预期行为?

如果object.ReferenceEquals返回true,应该instance.Equals总是返回true吗?

您认为下面的输出是通过/失败的意外行为吗?

Assert.True(object.ReferenceEquals(obj,obj));
Assert.True(obj.Equals(obj));
Run Code Online (Sandbox Code Playgroud)

就个人而言,我认为它很奇怪,并且无法想到一个很好的理由,一个实例不应该与自己相等.

c#

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

具有只读字段的TryParse模式/非默认构造函数

人们通常如何使用TryParse模式实现解析构造函数,当它们具有只读备份字段时,以及非默认构造函数,通常会进行解析?

下面是我正在谈论的一个人为的例子,以及我已经确定的模式,但看起来很笨拙.实际上,一些类型具有大量属性.当然,我可以创建一个方法来获取n参数,进行解析,并以这种方式连接它们,但在某些情况下使用带有15个参数的方法也是一种痛苦/恶臭.

两个构造函数的想法,加上必须将try解析的结果复制到解析构造函数中的只读字段,这有点令人闻到.

其他人有更好的模式吗?

编辑:提供更多上下文

我正在尝试做的是重构一个大的(ish)代码库,它有很多类型,如下面的例子,其中有解析提供给构造函数的字符串参数.现在,所有代码都使用构造函数解析,并且此解析的逻辑都在构造函数中完成.凌乱而又麻烦.

我想做的第一件事就是将这段代码从构造函数中移到工厂方法(TryParse)中,但是保留了构造函数签名,所以我对代码库没有太多的改动.从长远来看,有时间可以做得更好.

目前,难点在于保持现有代码的结构签名完整,同时允许新代码使用TryParse模式,并保留只读字段.如果我放松了readonly字段,整个过程会更容易,但我宁愿不这样做.

public class Point
{
   private readonly float x, y;
   public float X { get { return x; } }
   public float Y { get { return y; } }

   public Point(string s)
   {
      Point result;

      if (TryParse(s, out result))
      {
         this.x = result.x;
         this.y = result.y;
      }
      else
      {
         throw new System.ArgumentException("cant parse");
      }
   }

   private Point(float x,float y) // for the sake of the example, this wouldnt have any …
Run Code Online (Sandbox Code Playgroud)

c#

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

lisp defclass宏问题

一点点背景,我是一个完全没法比赛的人,几周前才开始,但我已经在其他领域开发了多年.逻辑没问题,口齿不清,问题.

我正在尝试编写一个宏,它将为我定义两个clsql类来解决库的问题.我想要命名的类x和`x-insert``,所以在宏中我想要宏来计算x-insert的符号名,但是我很难做到这一点.我的尝试在下面,但我对两件事感到难过.

如何创建类名称.如果我删除了空间,class -insert,它就不会评估,我理解,所以我认为我错过了一些直截了当的方式来告诉它忽略空间,并将名称创建为单个单词,第二个问题是将其转换为创建两个类,而不是一个,因为它只使用宏展开来扩展宏的最后一部分.

也许我完全以错误的方式解决这个问题,所以请随意向我推进正确的方向.

(defmacro gen-pair (class base-slots pkey-slot base-table)
  `(clsql:def-view-class ,class -insert()
     (
      ,base-slots
     )
     (:base-table ,base-table)
   )

  `(clsql:def-view-class ,class (,class -insert)
     (
      ,pkey-slot
     )
     (:base-table ,base-table)
   )
)
Run Code Online (Sandbox Code Playgroud)

lisp macros common-lisp

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

为什么我不能将BlockingCollection <T>分配给IReadOnlyCollection <T>?

有人可以解释为什么我得到以下编译错误?BlockingCollection<T>实现IReadOnlyCollectio<T>接口,其他接口没有问题.为什么要求显式演员,为什么我不必为此做同样的事List<T>

无法隐式转换'System.Collections.Concurrent.BlockingCollection<string>''System.Collections.Generic.IReadOnlyCollection<string>'.存在显式转换(您是否错过了演员?)

IReadOnlyCollection<string> roListItems = new List<string>(); // ok (baseline check)

IEnumerable<string> enumBCItems = new BlockingCollection<string>(); // ok
System.Collections.ICollection colBCItems = new BlockingCollection<string>(); // ok
IReadOnlyCollection<string> roBCItems = new BlockingCollection<string>(); // fail
IReadOnlyCollection<string> roExplicitBCItems = (IReadOnlyCollection<string>)new BlockingCollection<string>(); // ok....
Run Code Online (Sandbox Code Playgroud)

编辑

下面是resharper向我展示的内容,当我查看声明时,因此我的困惑.

// Type: System.Collections.Concurrent.BlockingCollection`1
// Assembly: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: BD5F7037-65C4-4C44-8FBC-F45D80D7550F
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll

public class BlockingCollection<T> : IEnumerable<T>, IEnumerable, ICollection, IDisposable, IReadOnlyCollection<T>
{ ... }
Run Code Online (Sandbox Code Playgroud)

c# .net-4.5

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