小编Ond*_*cek的帖子

必须在与DependencyObject相同的Thread上创建DependencySource

我有一个用wpf编写的应用程序,它可以下载一些网页,解析HTML代码并保存一些值.

class ListOfItems
{    
    public List<SomeObject> ListToBind;
    public void DownloadItems()
    { 
        Task.Factory.StartNew(() => 
        {
            ...
            ...
            if (OnDownloadCompleted != null)
                OnDownloadCompleted(this, EventArgs.Empty);
        }
    }
}

class SomeObject
{
    public string NameOfItem;
    public MyClass Properties;
}

class MyClass
{
    public int Percentage;
    public SolidColorBrush Color;
}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的对象模型.它是简化版本,我不希望你重新组织它,我有这样写道的原因.在ListOfItems类中是执行所有工作的方法(内部使用一些其他方法使代码可读) - 下载源,解析和填充ListToBind数据,fe

[0] => NameOfItem = "FirstOne", Properties = {99, #FF00FF00}
[1] => NameOfItem = "SecondOne", Properties = {50, #FFFF0000}
etc.
Run Code Online (Sandbox Code Playgroud)

如您所见,当此方法DownloadItems完成其作业时,OnDownloadCompleted将引发事件.在主线程中是以下代码

void listOfItems_OnDownloadCompleted(object sender, EventArgs …
Run Code Online (Sandbox Code Playgroud)

c# wpf datagrid multithreading dependency-properties

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

尽管没有实现接口,代码工作仍未实现

我有以下代码:

 interface ICalculator
 {
     int Sum ( int x,  int y);
 }

 abstract class AbsCalculator
 {
     public abstract int Sum(int x, int y);
 }

 class Calculator : AbsCalculator, ICalculator
 {
     public override int Sum(int x, int y)
     {
         Console.WriteLine ( "From overriden method" );
         return x + y;
     }
 }
Run Code Online (Sandbox Code Playgroud)

以下语句工作正常,程序编译,尽管我没有实现接口:

Calculator calculator = new Calculator();
Console.WriteLine ( calculator.Sum(10, 20) );
Run Code Online (Sandbox Code Playgroud)

所以我的第一个问题是:它为什么有用?

然后,我将以下接口实现添加到Calculator类:

int ICalculator.Sum(int x, int y)
{
     Console.WriteLine("From implemented method");
     return x + y;
}
Run Code Online (Sandbox Code Playgroud)

再次尝试以下声明时:

Calculator calculator …
Run Code Online (Sandbox Code Playgroud)

c# interface

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

正则表达式取代内部

好吧,我有这个代码:

        StreamReader sr = new StreamReader(@"main.cl", true);
        String str = sr.ReadToEnd();
        Regex r = new Regex(@"&");
        string[] line = r.Split(str);

        foreach (string val in line)
        {
            string Change = val.Replace("puts","System.Console.WriteLine()");
            Console.Write(Change);
        }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我正在尝试替换puts(内容),Console.WriteLine(content)但它需要正则表达式,我没有找到关于如何做这篇文章的好文章.

基本上,把*作为即将到来的价值,我想这样做:

string Change = val.Replace("puts *","System.Console.WriteLine(*)");
Run Code Online (Sandbox Code Playgroud)

然后,如果我收到:

puts "Hello World";
Run Code Online (Sandbox Code Playgroud)

我想得到:

System.Console.WriteLine("Hello World");
Run Code Online (Sandbox Code Playgroud)

c# regex

8
推荐指数
2
解决办法
454
查看次数

线程的ExecutionContext

目的是ExecutionContext.SuppressFlow();什么?在下面的代码中究竟被抑制了什么?

我有这个测试代码......

protected void btnSubmit_Click(object sender, EventArgs e)
{
   Thread[] th = new Thread[100];
   Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");

   AsyncFlowControl cntrl = ExecutionContext.SuppressFlow();
   for (int i = 0; i < th.Length; i++)
   {                   
      th[i] = new Thread(new ParameterizedThreadStart(ThreadMethod));
      th[i].Name = "Thread #" + (i+1).ToString();                
      th[i].Start((i+1).ToString());
   }
   ExecutionContext.RestoreFlow();

   foreach (Thread t in th)            
   {
      t.Join();
   }
   Response.Write(response);
}


String response = null;
Random rnd = new Random(1000);
private void ThreadMethod(object param)
{   
   if (param != null)
   {
      string temp …
Run Code Online (Sandbox Code Playgroud)

c# multithreading threadcontext

7
推荐指数
2
解决办法
9683
查看次数

从Nuget下载Unity IoC容器时出错

我正在构建一个控制台应用程序,以便控制反转.我试图从nuget包管理器下载"Unity"IoC容器,但是遇到了这个错误.任何帮助都很有用.我正在使用visual studio 2010 pro.

下载包时出错

c# unity-container nuget

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

将类类型作为参数c#传递并与字典一起使用

我创建一个函数来返回一个字典,我想通过作为参数传递类名.但它给出了一个错误.我写的代码如下

public Dictionary<object, object> GetDetails(Type Classname)
{
    MvcDemoDBEntities db = new MvcDemoDBEntities();
    Dictionary<Classname, object> dict = new Dictionary<Classname, object>();

    var data = (from h in db.People
                select h).ToList();

    foreach (var item in data)
    {
        dict.Add(data, true);
    }
    return dict;
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么,我想动态地用类名调用这个函数,如下所示:

List<people> list = GetDetails(people).Keys.ToList();
Run Code Online (Sandbox Code Playgroud)

人是我的班级名字.

c# parameters dictionary class

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

检查重复char上的字符串

我第一次写SO,因为他自己找不到解决方案.在采访中,我被赋予了编写一个方法来检查字符串中的字符是否唯一的任务.

要求:不使用LINQ.理想的:不要使用其他数据类型(Dictionary,HashSet等等.允许的数组和列表)

例:

"Hello" - return false; "Helo" - return true
Run Code Online (Sandbox Code Playgroud)

我的实施:

static HashSet<char> charSet = new HashSet<char>();

static bool IsUniqueChar(string str)
{       
    foreach (char c in str)
    {            
           charSet.Add(c);                         
    }
    return charSet.Count() == str.Length;
}
Run Code Online (Sandbox Code Playgroud)

但它不符合数据类型的要求,并不是最好的表现......我也尝试过用字典方法:

static Dictionary<char,bool> charSetDictionary = new Dictionary<char,bool>();

static bool IsUniqueChar(string str)
{
    try
    {
        foreach (char c in str)
        {
            charSetDictionary.Add(c,true);
        }
    }
    catch 
    {
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

但他并不比以前更好.我会欢迎任何想法如何更好地解决这个任务?PS

static void Main(string[] args)
{
    Stopwatch sw = …
Run Code Online (Sandbox Code Playgroud)

c# string duplicates

7
推荐指数
2
解决办法
964
查看次数

复选框绑定 - 什么是更好的解决方案?

我的wpf格式中有20多个复选框.我需要IsChecked在某些对象中存储所有这些值.

我知道两种方式.

1)使用依赖属性将所有复选框绑定到对象中的相应属性,如此处所示

2)处理Clicked所有这些事件

哪种解决方案更好?是否有更好的解决方案在代码隐藏中占用更少的空间?

c# data-binding wpf checkbox events

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

Visual Studio 2013功能与NUnit的代码镜头

在新的Visual Studio 2013中有一个很好的新功能,名为Code Lens(我认为它只在Ultimate版本中).关于方法,除了显示引用(多少和哪里)之外,它还显示了在团队工作和测试统计信息时有关版本控制的一些信息,以便您知道使用该方法的测试通过了多少次以及失败多少次.

我认为这与嵌入在Visual Studio中的测试项目完美配合,但它是否适用于NUnit?我们正在使用NUnit进行单元测试,我们正在尝试决定是否迁移到VS2013,其中一个原因就是这个新功能,如果它支持NUnit,我们肯定会充分利用它.

你有经验吗?

.net nunit unit-testing visual-studio-2013 codelens

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

ReSharper 检查:未知工具版本 17.0

运行 TeamCity ReSharper 检查命令行工具时。存在错误“未知工具版本 17.0”,详细信息如下。

Inspections (ReSharper)
  Unknown tools version: 17.0
  
  --- EXCEPTION #1/1 [LoggerException]
  Message = "Unknown tools version: 17.0"
  ExceptionPath = Root
  ClassName = JetBrains.Util.LoggerException
  HResult = COR_E_APPLICATION=80131600
Run Code Online (Sandbox Code Playgroud)

我们注意到在安装 VS2022 或 VS2022 构建工具来构建代理后会发生这种情况。有办法解决吗?

TeamCity 企业版 2021.2.2(内部版本 99660)

resharper teamcity continuous-integration code-inspection

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