Tag: .net

WinForms interthread修改

每当我想从另一个线程修改winform时,我需要使用 ->Invoke(delegate, params) 所以修改发生在winform自己的线程中。 对于需要修改gui的每个函数,我需要另一个委托函数。 是否有一些方案允许我限制所需的委托function的数量? 我有一个控制器类,可以在一个地方处理整个gui,我考虑过重用代表但是闻起来很糟糕。 我认为我的问题可以适用于winform可以运行的所有语言

如何在RichTextBox中的文本上正确应用backgroundcolor

internal string Select(RichTextBox rtb, int index, int length) { TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd); if (textRange.Text.Length >= (index + length)) { TextPointer start = textRange.Start.GetPositionAtOffset(index, LogicalDirection.Forward); TextPointer end = textRange.Start.GetPositionAtOffset(index + length, LogicalDirection.Backward); rtb.Selection.Select(start, end); rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.LightBlue)); } return rtb.Selection.Text; } 每当调用ApplyPropertyValue来更改所选文本的背景颜色时,它首次运行良好,但在第二次调用时不能正确调整所选文本段的背景颜色。 我怀疑这与调用函数后文件的偏移量有些混乱有关。 解决这个问题的好方法是什么?

检查用户是域用户还是本地用户

我希望能够检查给用户名,如果该用户是机器用户或本地用户(使用.NET优先)的机器,但可以在网上找到很多 public static Boolean isLocalUser (string name) { //code here } 编辑例如你被赋予me.user作为字符串

为什么我收到错误“类型’IReturn ‘是在未引用的程序集中定义的”使用VIsualStudio 2017中的ServiceStack

我正在运行Visual Studio 2017 15.6.3。 我有一个.NET Standard 2.0 DLL项目,其中包含与ServiceStack一起使用的Request和Response类。 Request类实现了IReturn 。 我有一个.NET Core 2.0控制台EXE项目,它引用了.NET标准DLL。 此EXE使用ServiceStack JsonServiceClient将请求发送到ServiceStack服务。 它编译并正常工作。 我添加了一个.NET Framework 4.6.1控制台EXE项目,该项目也引用了.NET Standard 2.0 DLL。 它必须是一个Framework应用程序,因为它引用了与Core或Standard不兼容的其他DLL。 此EXE使用ServiceStack JsonServiceClient将请求发送到与.NET Core EXE完全相同的ServiceStack服务,但此程序将无法编译。 框架4.6.1应该支持.NET Standard 2.0 DLL,但由于某种原因它与IReturn 接口有冲突。 var extentRequest = new ExtentRequest { … }; using (var client = new JsonServiceClient(baseUrl)) { return client.Post(extentRequest); } 返回的错误是:“类型’IReturn ‘在未引用的程序集中定义。您必须添加对程序集’ServiceStack.Interfaces,Version = 5.0.0.0,Culture = […]

编译器是否继续评估一个表达式,如果第一个为false,则all必须为true?

我确定此问题可能已经得到了回答,所以我道歉,但我找不到合适的搜索条件来找到答案。 给出以下代码示例, db.GetRecords().Any()被执行? string s = “Z”; bool x = s.IndexOfAny(new[] { ‘A’, ‘B’ }) > 0 && db.GetRecords().Any();

C#如何序列化system.linq.expressions?

我正在研究winRT和entity framework(到SQL),它们之间进行通信的层是WCF服务。 在entity framework中,我使用的是Repository Pattern,我有方法: public IQueryable GetBySearch(Expression<Func> search) { return this.Context.Users.Where(search); } 一切正常,但是当我把它添加到WCF [OperationContract] IQueryable GetUserBySearch(Expression<Func> search); 和: public IQueryable GetUserBySearch(Expression<Func> search) { IUser user = new UserRepository(); return user.GetBySearch(search); } 但是Expression不能序列化的问题,因此,WCF无法序列化它。 所以我想inheritance它并使其成为[Serializable]但问题是它是一个密封的类。 有人可以帮我解决问题吗?

Wson中的JsonConvert.DeserializeObject和“d”包装器

默认情况下,WCF服务在“d”包装器中包装JSON响应,在那里我发现解析它有问题。 如果我用JsonConvert.DeserializeObject(响应)解析响应 “{\”d\”:\”{\”a0b70d2f-7fe4-4aa2-b600-066201eab82d\”:\”Thelma\”,\”d56d4d4f-6029-40df-a23b-de27617a1e43\”:\”Louise\”}\”}” 我错了: After parsing a value an unexpected character was encoutered: a. Line 1, position 9. 如果我改变响应 “{\”a0b70d2f-7fe4-4aa2-b600-066201eab82d\”:\”Thelma\”,\”d56d4d4f-6029-40df-a23b-de27617a1e43\”:\”Louise\”}” 我搞定了。 那么如何从WCF服务解析这个“d”包装的JSON响应呢? 有没有更好的方法来解析JSON?

使用Google Drive API获取文件夹层次结构

我正在寻找一种优雅的方式来获取文件夹层次结构,从我的根文件夹开始,使用C#Google Drive API V3。 目前,您可以通过以下方式获取根文件夹及其父文件夹 var getRequest = driveService.Files.Get(“root”); getRequest.Fields = “parents”; var file = getRequest.Execute(); 但我正在寻找一种方法来获得孩子,而不是父母,所以我可以递归下去文件结构。 设置getRequest.Fields = ‘children’不是有效的字段选项。

在httpwebrequest中创建TCP连接的方式和位置,以及它与servicepoint的关系如何?

我试图找出在使用HttpWebRequest时何时建立TCP连接,如何使用ServicePoint汇集和重用这些连接。 我查看了system.dll,并尝试使用ILSpy和Reflector浏览代码,不知何故没有看到任何对套接字的引用,建立tcp连接等。 下面我已经粘贴了反编译的代码 – 任何请给我提示或重定向我,以便我能理解: 什么时候创建TCP连接? 如何使用ServicePoint保持这些连接的存活,汇集和重用? HttpWebRequest of System.dll的代码片段: public override Stream GetRequestStream() { TransportContext context; return this.GetRequestStream(out context); } public Stream GetRequestStream(out TransportContext context) { if (Logging.On) { Logging.Enter(Logging.Web, this, “GetRequestStream”, “”); } context = null; this.CheckProtocol(true); if ((this._WriteAResult == null) || !this._WriteAResult.InternalPeekCompleted) { lock (this) { if (this._WriteAResult != null) { throw […]

初始化List上的ArgumentOutOfRangeException

它在For循环的中间抛出一个ArgumentOutOfRangeException,请注意我删除了for循环的其余部分 for (int i = 0; i < CurrentUser.Course_ID.Count – 1; i++) { CurrentUser.Course[i].Course_ID = CurrentUser.Course_ID[i]; } 课程代码是 public class Course { public string Name; public int Grade; public string Course_ID; public List Direct_Assoc; public List InDirect_Assoc; public string Teacher_ID; public string STUTeacher_ID; public string Type; public string Curent_Unit; public string Period; public string Room_Number; […]