小编Ant*_*ols的帖子

StreamReader和可移植类库

我正在使用Portable Class Libraries编写ConfigManager类.PCL支持StreamReaderStreamWriter我想要使​​用的类,但这些类的PCL版本不支持在构造期间传入字符串.PCL也不支持reader.Close()writer.Close().最后它不支持该FileStream课程.

所以我正在寻找以下任何一个问题的答案:

  1. 我怎样才能让这些StreamReaderStreamWriter类在PCL中工作?
  2. 如何stream使用PCL 创建新的?
  3. 还有什么其他的替代品可以在PCL中加载和保存文件?

c# filestream streamwriter streamreader portable-class-library

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

在C#中为类添加一个否定运算符(?)

我可能没有在这里使用正确的词语,这可能就是我无法自己找到答案的原因.我班上有一个+& - 运算符,但我想添加一个负运算符...我基本上可以做到这一点:

myMethod(myClass, -myClass);

如果你需要代码示例来帮助我让我知道,但我认为这应该是相当紧张的前进......或者说它无法完成.

c# operators

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

C#合并两个SortedLists(Union?)

我正在寻找加速合并两个代码的代码SortedLists.

C#4.0通用SortedList:http://msdn.microsoft.com/en-us/library/ms132319( v = vs.100).aspx

public Trait getTrait(decimal thisValue)
{       
    if (ParentStructure != null && ParentStructure.RankedTraits.Count > 0)
    {
        SortedList<decimal, Trait> tempTraits = this.RankedTraits;

        // Improve here (union?)
        foreach (KeyValuePair<decimal, Trait> kvp in (ParentStructure.RankedTraits))
        {
            if (!tempTraits.ContainsKey(kvp.Key)) 
            { 
                tempTraits.Add(kvp.Key, kvp.Value); 
            }
        }
        return _getTrait(tempTraits, thisValue);
        }
    }
    return _getTrait(_rankTraits, thisValue);
}
Run Code Online (Sandbox Code Playgroud)

我认为联合而不是foreach循环会更快,但我不知道如何实现一个联合SortedList.如果有人可以帮助我,我会很感激.

此外,如果有更好的方法来做到这一点,我愿意接受建议.

c# sortedlist c#-4.0

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

从多个(n)列表生成所有组合

编辑:我完全重做了我的问题,因为我已经找到了最简单的问题.感谢到目前为止的评论者让我思考根本问题.

public List<string> GetAllPossibleCombos(List<List<string>> strings)
{
    List<string> PossibleCombos = new List<string>();

    //????
    {
        string combo = string.Empty;
        // ????
        {
            combo += ????
        }
        PossibleCombos.Add(combo);
    }

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

我需要弄清楚如何递归遍历每个List<string>并将每个列表中的1个字符串组合成一个组合string.不要过于担心格式化字符串,因为"实时"代码使用自定义对象.此外,请随意假设每个列表至少包含1个字符串,并且没有空值.

c# linq dictionary list

5
推荐指数
3
解决办法
3560
查看次数

在C#中添加long和ulong

我正在使用的api有一个ulong字段(我不能改变它).我需要调整这个字段的long数量(可能int也是).但是我不能添加这些togeather : Operator '+' is ambiguous on operands of type 'ulong' and 'long'.

我无法转换long为a ulong因为我将失去标志,我无法将其转换ulong为a long因为我可能会丢失部分值.

最终我想尝试调整值,如果它会导致我想要返回的换行false并且没有完成调整.如果它没有换行我想完成调整并返回true; 我可以做这个部分,但前提是我可以找到一种方法将两个字段放在一起.

public ulong Value;
public bool AdjustValue(long amount)
{
    // Operator '+' is ambiguous on operands of type 'ulong' and 'long'
    ulong adjustValue = Value + amount; 

    if (amount < 0 && adjustValue > Value)
        return false;
    if (amount > 0 && adjustValue < Value)
        return false;

    Value …
Run Code Online (Sandbox Code Playgroud)

c# math

5
推荐指数
2
解决办法
1454
查看次数

加载文件不起作用 - GZip标头中的幻数不正确

我正在尝试创建一个Save/Load类,它具有保存和加载文件压缩文件的选项.以下是我到目前为止的情况.单步执行它似乎工作得很好,除了我得到"GZip标头中的幻数不正确"异常.我不明白这是怎么回事,因为我检查确保数字在我传递之前存在,并且我已经通过外部程序验证它是GZip文件.

任何协助找出我出错的地方将不胜感激.对我的代码的建设性批评总是受欢迎的 - 谢谢!

public static class SaveLoad
{
    public static void Save(string fileName, object savefrom, bool compress)
    {
        FileStream stream = new FileStream(fileName, FileMode.Create);

        BinaryFormatter formatter = new BinaryFormatter();
        if (compress)
        {
            GZipStream compressor = new GZipStream(stream, CompressionMode.Compress);
            formatter.Serialize(compressor, savefrom);
            compressor.Close();
        }
        else { formatter.Serialize(stream, savefrom); }

        stream.Close();
    }

    public static object Load(string fileName)
    {
        object loadedObject = null;

        try
        {
            FileStream stream = new FileStream(fileName, FileMode.Open);

            BinaryFormatter formatter = new BinaryFormatter();

            if (stream.Length > 4)
            {
                byte[] data …
Run Code Online (Sandbox Code Playgroud)

c# serialization gzip binaryformatter deserialization

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

在 WPF 应用程序中,您如何覆盖控制台关闭命令?

在我的 WPF 应用程序中,我使用控制台窗口作为调试/消息窗口 - 我有选项设置来根据用户的需要显示和隐藏窗口,一切都很好。唯一的问题是,当用户关闭控制台窗口时,它会退出整个程序。如何覆盖它,以便当用户单击 X 时它只是隐藏控制台而不是退出应用程序?

这是我到目前为止:

const int SW_HIDE = 0;
const int SW_SHOW = 5;
public static bool ConsoleVisible { get; private set; }

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);


public static void HideConsole()
{
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_HIDE);
    ConsoleVisible = false;

}
public static void ShowConsole()
{
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_SHOW);
    ConsoleVisible = true;
}
Run Code Online (Sandbox Code Playgroud)

** 对于想要使用它的人,您需要: using System.Runtime.InteropServices;

此代码源自此答案:https : //stackoverflow.com/a/3571628/649382

** 编辑 …

c# wpf console

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

Booleen检查C#; 转换代码

如果找到这段代码:

int error;
if (error > 0)
{
    if (error || (move_y > 0))
    {
        los_x_1 += move_x;
        error -= delta_y;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里:http://roguebasin.roguelikedevelopment.org/index.php?title = Other_version_of_BLA

我认为代码是在C#中,但上面的代码块不起作用; 我在想它正在检查它是否'错误== 1',但我不确定.有任何想法吗?

c# int boolean

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