Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。



9
是否可以“等待收益回报DoSomethingAsync()”
常规迭代器块(即“收益回报”)是否与“异步”和“等待”不兼容? 这样可以很好地说明我要做什么: async Task<IEnumerable<Foo>> Method(String [] Strs) { // I want to compose the single result to the final result, so I use the SelectMany var finalResult = UrlStrings.SelectMany(link => //i have an Urlstring Collection await UrlString.DownLoadHtmlAsync() //download single result; DownLoadHtmlAsync method will Download the url's html code ); return …


2
将文件转换为Base64String然后再次返回
标题说明了一切: 我像这样读tar.gz档案 将文件分成字节数组 将这些字节转换为Base64字符串 将该Base64字符串转换回字节数组 将那些字节写回到新的tar.gz文件中 我可以确认两个文件的大小相同(以下方法返回true),但是我无法再提取副本版本。 我想念什么吗? Boolean MyMethod(){ using (StreamReader sr = new StreamReader("C:\...\file.tar.gz")) { String AsString = sr.ReadToEnd(); byte[] AsBytes = new byte[AsString.Length]; Buffer.BlockCopy(AsString.ToCharArray(), 0, AsBytes, 0, AsBytes.Length); String AsBase64String = Convert.ToBase64String(AsBytes); byte[] tempBytes = Convert.FromBase64String(AsBase64String); File.WriteAllBytes(@"C:\...\file_copy.tar.gz", tempBytes); } FileInfo orig = new FileInfo("C:\...\file.tar.gz"); FileInfo copy = …

4
xUnit:断言两个List <T>是否相等?
我是TDD和xUnit的新手,所以我想测试一下如下所示的方法: List&lt;T&gt; DeleteElements&lt;T&gt;(this List&lt;T&gt; a, List&lt;T&gt; b); 有什么我可以使用的断言方法吗?我觉得这样会很好 List&lt;int&gt; values = new List&lt;int&gt;() { 1, 2, 3 }; List&lt;int&gt; expected = new List&lt;int&gt;() { 1 }; List&lt;int&gt; actual = values.DeleteElements(new List&lt;int&gt;() { 2, 3 }); Assert.Exact(expected, actual); 有这样的东西吗?
108 c#  xunit 

3
继承如何作用于属性?
Inherited属性的bool属性指的是什么? 这是否意味着如果我使用属性定义了我的类AbcAtribute(具有Inherited = true),并且从该类继承了另一个类,则派生类也将具有相同的属性? 为了用一个代码示例来阐明这个问题,请想象以下内容: [AttributeUsage(AttributeTargets.Class, Inherited = true)] public class Random: Attribute { /* attribute logic here */ } [Random] class Mother { } class Child : Mother { } 是否Child也Random应用了属性?
108 c#  .net  vb.net  attributes 

9
动态替换C#方法的内容?
我想做的是更改C#方法在调用时的执行方式,以便我可以编写如下内容: [Distributed] public DTask&lt;bool&gt; Solve(int n, DEvent&lt;bool&gt; callback) { for (int m = 2; m &lt; n - 1; m += 1) if (m % n == 0) return false; return true; } 在运行时,我需要能够分析具有Distributed属性(我已经可以做到)的方法,然后在函数主体执行之前和函数返回之后插入代码。更重要的是,我需要能够在不修改调用Solve或在函数开始的地方修改代码(在编译时;在运行时这样做才是目标)。 目前,我尝试了这段代码(假设t是Solve的存储类型,而m是Solve的MethodInfo): private void WrapMethod(Type t, MethodInfo m) { // Generate ILasm for delegate. byte[] il = …
108 c#  methods  assemblies  cil  swap 

2
如何从httpclient调用中获取内容主体?
我一直在尝试弄清楚如何读取httpclient调用的内容,但似乎无法理解。我得到的响应状态是200,但是我不知道如何获得返回的实际Json,这就是我所需要的! 以下是我的代码: async Task&lt;string&gt; GetResponseString(string text) { var httpClient = new HttpClient(); var parameters = new Dictionary&lt;string, string&gt;(); parameters["text"] = text; Task&lt;HttpResponseMessage&gt; response = httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters)); return await response.Result.Content.ReadAsStringAsync(); } 我得到它只是从方法中调用它: Task&lt;string&gt; result = GetResponseString(text); 这就是我得到的 response Id = 89, Status = RanToCompletion, Method = "{null}", Result = "StatusCode: …

5
调试时如何使用参数启动程序?
我想在Visual Studio 2008中调试程序。问题是,如果不获取参数,它将退出。这是从主要方法: if (args == null || args.Length != 2 || args[0].ToUpper().Trim() != "RM") { Console.WriteLine("RM must be executed by the RSM."); Console.WriteLine("Press any key to exit program..."); Console.Read(); Environment.Exit(-1); } 我不想将其注释掉,然后在编译时再注释掉。调试时如何使用参数启动程序?它被设置为启动项目。

10
如何通过字符串或整数获取枚举值
如果我有枚举字符串或枚举int值,如何获取枚举值。例如:如果我有如下枚举: public enum TestEnum { Value1 = 1, Value2 = 2, Value3 = 3 } 在某些字符串变量中,我的值是“ value1”,如下所示: string str = "Value1" 或在一些int变量中,我的值是2 int a = 2; 我如何获得enum的实例?我想要一个通用方法,可以在其中提供枚举和输入字符串或int值来获取枚举实例。
108 c#  enums 

12
如何解决用户控件中的闪烁
在我的应用程序中,我不断地从一个控件转移到另一个控件。我没有创建。用户控件,但在导航过程中我的控件会闪烁。更新需要1或2秒。我试图设置这个 SetStyle(ControlStyles.OptimizedDoubleBuffer, true); or SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); 但这无济于事...每个控件都有相同的背景图像,但控件不同。那么解决方案是什么.. 谢谢。

11
为什么“ continue”语句不能在“ finally”块内?
我没问题 我只是好奇。想象以下情况: foreach (var foo in list) { try { //Some code } catch (Exception) { //Some more code } finally { continue; } } 这不会编译,因为它会引起编译器错误CS0157: 控制不能离开finally子句的主体 为什么?
107 c#  .net 

12
将以10为基数的数字转换为.NET中任何基数的最快方法?
我有一个旧的C#方法,该方法接受一个数字并将其转换为任何基数: string ConvertToBase(int number, char[] baseChars); 并不是所有的东西都那么快速和整洁。在.NET中是否有一种良好的已知方法来实现这一目标? 我正在寻找可以使我使用任何带有任意字符串的基数的东西。 这仅允许以16、10、8和2为基数: Convert.ToString(1, x); 我想利用它来利用数字,所有小写字母和所有大写字母来获得很高的基础。就像在这个线程中一样,但是对于C#来说不是JavaScript。 有谁知道用C#做到这一点的好方法吗?
107 c#  .net  int  base  number-systems 

8
在泛型类型参数上调用静态方法
我希望这样做,但是在C#中似乎是非法的: public Collection MethodThatFetchesSomething&lt;T&gt;() where T : SomeBaseClass { return T.StaticMethodOnSomeBaseClassThatReturnsCollection(); } 我收到一个编译时错误:“'T'是一个'类型参数',在给定的上下文中无效。” 给定通用类型参数,如何在通用类上调用静态方法?给定约束,静态方法必须可用。
107 c#  generics 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.