小编Arm*_*rat的帖子

Mono中的WCF支持

我试图找出Mono下WCF支持和不支持的内容.我已经在Mono Project页面上阅读了WCF开发文档.

对于有在Mono下使用WCF的经验的人,我应该注意哪些问题?

c# mono wcf

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

使用GetHashCode测试Equals覆盖中的相等性

可以调用GetHashCode作为从Equals覆盖内部测试相等性的方法吗?

例如,这段代码可以接受吗?

public class Class1
{
  public string A
  {
    get;
    set;
  }

  public string B
  {
    get;
    set;
  }

  public override bool Equals(object obj)
  {
    Class1 other = obj as Class1;
    return other != null && other.GetHashCode() == this.GetHashCode();
  }

  public override int GetHashCode()
  {
    int result = 0;
    result = (result ^ 397) ^ (A == null ? 0 : A.GetHashCode());
    result = (result ^ 397) ^ (B == null ? 0 : B.GetHashCode());
    return result;
  }
}
Run Code Online (Sandbox Code Playgroud)

c# gethashcode

9
推荐指数
2
解决办法
3073
查看次数

使用Thread.Abort杀死HttpWebRequest对象

所有,我试图使用类似于下面的代码的方法取消两个并发的HttpWebRequests(以伪ish C#显示).

Main方法创建两个创建HttpWebRequests的线程.如果用户希望,他们可以通过单击按钮然后调用Abort方法来中止请求.

private Thread first;
private Thread second;
private string uri = "http://somewhere";

public void Main()
{
  first = new Thread(GetFirst);
  first.Start();

  second = new Thread(GetSecond);
  second.Start();

  // Some block on threads... like the Countdown class
  countdown.Wait();
}

public void Abort()
{
  try
  {
    first.Abort();
  }
  catch { // do nothing }

  try
  {
    second.Abort();
  }
  catch { // do nothing }
}

private void GetFirst(object state)
{
  MyHandler h = new MyHandler(uri);
  h.RunRequest();
}

private void GetSecond(object state) …
Run Code Online (Sandbox Code Playgroud)

c# multithreading httpwebrequest

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

GZipStream的解压缩性能很差

我有一个连接到后端WAS服务器的.NET 2.0 WinForms应用程序。我正在使用GZipStream解码从对服务器进行的HttpWebRequest调用返回的数据。返回的数据是Apache压缩的压缩CSV。整个服务器堆栈是Hibernate-> EJB-> Spring-> Apache。

对于较小的响应,性能很好(<50ms)。当我收到> 150KB的响应时,解压缩需要60秒钟以上。大部分时间似乎都花在了GZipStream构造函数上。

这是显示从HttpWebResponse调用获取响应流的代码:

using (Stream stream = this.Response.GetResponseStream())
{
 if (this.CompressData && this.Response.ContentEncoding == "gzip")
 {
        // Decompress the response
  byte[] b = Decompress(stream);
  this.ResponseBody = encoding.GetString(b);
    }
 else
 {
  // Just read the stream as a string
  using (StreamReader sr = new StreamReader(stream))
  {
   this.ResponseBody = sr.ReadToEnd();
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

编辑1

基于Lucero的评论,我将Decompress方法修改为以下内容,但是在实例化GZipStream之前,将ResponseStream加载到MemoryStream中不会带来任何性能优势。

private static byte[] Decompress(Stream stream)
{
 using (MemoryStream ms = new MemoryStream())
 {
  byte[] buffer = new byte[4096];
  int read = …
Run Code Online (Sandbox Code Playgroud)

c# compression performance gzipstream

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

类继承命名

我想继承DevExpress ComboBoxEdit控件,我想知道如果命名我的类与DevExpress类相同是不好的做法.

这是派生类声明:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyApplication.Components
{
    public class ComboBoxEdit : DevExpress.XtraEditors.ComboBoxEdit
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该将MyApplication.Components.ComboBoxEdit重命名为MyComboBoxEdit吗?

c# inheritance devexpress

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

将泛型集合转换为C#2.0中的具体实现

选择解决方案

感谢大家的帮助.我决定做以下事情.

public static class PersonCollection
{
    public static List<string> GetNames(RecordCollection<Person> list)
    {
        List<string> nameList = new List<string>(list.Count);

        foreach (Person p in list)
        {
            nameList.Add(p.Name);
        }

        return nameList;
    }
}
Run Code Online (Sandbox Code Playgroud)



我正在尝试将一个通用集合RecordCollection转换为派生集合PersonCollection,但是我得到了一个强制转换异常:

RecordCollection<Person> col = this.GetRecords<Person>(this.cbPeople);
PersonCollection people = (PersonCollection)col;
Run Code Online (Sandbox Code Playgroud)

我试图这样做的原因有两个:

  1. 派生类(例如,PersonCollection)可以具有不应该在基类中的实例方法(例如,GetLastNames).
  2. GetRecords方法是通用的,因此我可以获得任何Record对象的集合.

在C#2.0中解决这个问题的最佳方法是什么?解决这个问题最优雅的方法是什么?

这是GetRecords的签名:

public RecordCollection<T> GetRecords<T>(ComboBox cb) where T : Record, new()
Run Code Online (Sandbox Code Playgroud)

这是我的基本实现:

public abstract class Record : IComparable
{
    public abstract int CompareTo(object other);
}

public class RecordCollection<T> : ICollection<T> where T : Record, …
Run Code Online (Sandbox Code Playgroud)

c# generics collections casting c#-2.0

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

如何使用.NET XML序列化将对象序列化为单个值

假设我有以下内容:

[Serializable]
public class Foo
{
    public Bar bar
    {
        get;
        set;
    }

    public Ram ram
    {
        get;
        set;
    }
}

[Serializable]
public class Bar
{
    [XmlElement("barId")]
    public int Id
    {
        get;
        set;
    }
}

[Serializable]
public class Ram
{
    [XmlElement("ramId")]
    public int RamId
    {
        get;
        set;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想序列化为XML:

<Foo>
    <barId>123</barId>
    <ramId>234</ramId>
</Foo>
Run Code Online (Sandbox Code Playgroud)

做这个的最好方式是什么?

我想我必须创建一个序列化的中间类.备择方案?

c# xml serialization

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