草庐IT

lboost_system-mt

全部标签

C#:System.Object 与泛型

我很难理解何时使用对象(装箱/拆箱)与何时使用泛型。例如:publicclassStack{intposition;object[]data=newobject[10];publicvoidPush(objecto){data[position++]=o;}publicobjectPop(){returndata[--position];}}对比publicclassStack{intposition;T[]data=newT[100];publicvoidPush(Tobj){data[position++]=obj;}publicTPop(){returndata[--positi

c# - 无法确定条件表达式的类型,因为 'string' 和 'System.DBNull' 之间没有隐式转换

param7[1].Value=tbLastName.Text.Length>0?tbLastName.Text:DBNull.Value;为什么这是不可能的,还有什么建议可以尝试吗?20个参数的If/else是不行的。 最佳答案 问题是(如错误消息所示)条件表达式在两个分支上需要相同的类型,否则需要从一种类型到另一种类型的隐式转换。在你的情况下,没有一个,所以你得到了错误。一种快速修复方法是将其中一个值转换为(object)(这很好,因为DbParameter.Value无论如何都是object类型。)所以这应该适合你:param

c# - 为什么 System.IO.File.Exists(string path) 返回 false?

System.IO.File.Exists(stringpath)始终返回false,即使文件存在于指定路径中也是如此。可能的解决方案是什么? 最佳答案 这很可能是权限问题。来自documentation:TheExistsmethodreturnsfalseifanyerroroccurswhiletryingtodetermineifthespecifiedfileexists.Thiscanoccurinsituationsthatraiseexceptionssuchaspassingafilenamewithinvalid

c# - 引用 System.Windows.Automation

我想尝试通过BugSlayer获得的一些自动化功能,但在引用方面遇到问题。我正在尝试弄清楚如何在VisualStudio中引用System.Windows.Automation库。我安装了.NETFrameworkv3.5和VS2008,但UIAutomationClient和System.Windows.Automation都没有作为引用选项出现在.NET或COM选项卡中以供引用。如果我通过命令提示符导航到C:\WINDOWS\assembly\GAC_MSIL\UIAutomationClient\3.0.0.0__31bf3856ad364e35,我可以看到dll,但是如果我在V

c# - 什么可能导致 Visual Studio 单元测试中出现 System.TypeLoadException?

我有一个编译良好的C#.NET类库MyClassLibrary。我正在尝试为其创建一个单元测试项目(使用VisualStudio单元测试框架和VisualStudio2010)。类库中确实有大类,但每当我对最简单的类运行最简单的测试时,我都会收到以下异常:TestmethodMyClassLibraryTest.MyClassLibraryTests.MySimpleClassTestthrewexception:System.TypeLoadException:Couldnotloadtype'MyClassLibrary.MySimpleClass'fromassembly'MyC

c# - AutomationElement 使用 Inspect.exe 显示,但在使用 UIAutomationCore.dll 或 System.Windows.Automation 时不显示

TL;DR:我做错了什么导致工作区Pane出现在InspectObjects中但没有出现在我的自定义代码中?我正在尝试为第3方程序编写一些UI自动化。我正在使用Inspect.exeWindowsSDK附带的,我都试过了System.Windows.Automation并直接COMCalls(使用来自UIAVerify的包装器库)。Process[]processes=Process.GetProcessesByName("RedactedClient");if(processes.Length==0)thrownewException("Couldnotfind\"RedactedC

c# - 计算 BigInteger 的平方根 (System.Numerics.BigInteger)

.NET4.0为任意大的整数提供了System.Numerics.BigInteger类型。我需要计算BigInteger的平方根(或合理的近似值——例如整数平方根)。这样我就不必重新实现轮子,有人对此有很好的扩展方法吗? 最佳答案 CheckifBigIntegerisnotaperfectsquare具有计算JavaBigInteger的整数平方根的代码。这里翻译成C#,作为扩展方法。publicstaticBigIntegerSqrt(thisBigIntegern){if(n==0)return0;if(n>0){intbi

c# - 无法在 .NET FX4.6 上的 .NET Standard2.0 库中使用 System.Configuration.Configuration 管理器

我在NetStandard2.0中创建了一个程序集。它使用System.Configuration.ConfigurationManager读取AppSettings。我已经安装了System.Configuration.ConfigurationManager的nuget包,版本为4.4.X,适用于NetStandard2.0。当我在控制台应用程序(.NetCore)中引用此程序集时,它正在正确读取AppSettings,但是当我在旧的.NetFramework(4.6.X)控制台应用程序中引用此程序集时,它无法正常工作并引发异常。请看下面的代码。程序集1:NetStandard2.

c# - 命令绑定(bind)无法将类型为 'System.Reflection.RuntimeEventInfo' 的对象转换为类型 'System.Reflection.MethodInfo'

当我通过XAML将按钮连接到命令时,出现运行时错误System.Windows.Markup.XamlParseException:在“System.Windows.Data.Binding”上提供值引发异常。--->System.InvalidCastException:无法将“System.Reflection.RuntimeEventInfo”类型的对象转换为类型“System.Reflection.MethodInfo”。当我删除XAML中的命令绑定(bind)时,一切正常,我的项目显示等。这是命令绑定(bind):Click="{BindingElementName=Main

c# - NewGuid 与 System.Guid.NewGuid().ToString ("D");

使用NewGuid();与System.Guid.NewGuid().ToString("D");生成GUID时有区别吗?或者它们是一样的吗? 最佳答案 我意识到这个问题已经有了一个可接受的答案,但我认为分享一些关于格式化guid的信息会很有用。ToString()(noparameters)method使用此格式格式化guid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxToString(stringformat)method以多种方式之一格式化guid:"N"-xxxxxxxxxxxxxxxxxxxx