Tag: c#

将命名空间传递给函数

我有一个函数,它接受一个word文档并以html格式保存。 我想使用相同的函数来处理任何文档类型。 由于Jon Skeet指出,我尝试使用generics(我假设不同的doc API是相同的)失败了。 还有另一种方式吗? using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; //Works ok private void convertDocToHtm( string filename ) { … snip var app = new Word.Application(); var doc = new Word.Document(); doc = app.Documents.Open(ref fileName, ref missing, ref trueValue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, […]

如何让KeyedCollection成为只读?

首先让我解释为什么我使用KeyedCollection。 我正在构建一个DLL,我有一个项目列表,我需要添加到一个集合,并让它们按照我放置它们的顺序保留,但我还需要通过它们的索引和键来访问它们(关键是我已定义的对象的属性)。 如果有任何其他更简单的集合,请告诉我。 好了,我需要能够在DLL内部向此集合中添加项目,但是我需要将它作为只读公开提供给DLL的最终用户,因为我不希望它们删除/更改我添加的项目。 我一直在这个网站,其他网站,谷歌搜索,我一直无法找到一种方法来获得某种只读的KeyedCollection。 我最接近的是这个页面( http://www.koders.com/csharp/fid27249B31BFB645825BD9E0AFEA6A2CCDDAF5A382.aspx?s=keyedcollection#L28 ),但我无法让它发挥作用。 更新: 我看了看那些C5课程。 这与你的其他评论一起帮助我更好地理解如何创建我自己的只读类,它似乎工作。 但是,当我尝试将常规版本转换为只读版本时,我遇到了问题。 我得到一个编译时无法转换错误。 这是我创建的代码(第一个小类是我最初的代码): public class FieldCollection : KeyedCollection { protected override string GetKeyForItem(Field field) { return field.Name; } } public class ReadOnlyFieldCollection : KeyedCollection { protected override string GetKeyForItem(Field field) { return field.Name; } new public void Add(Field field) { throw new ReadOnlyCollectionException(“This collection […]

DI和存储库模式

目前,我的代码与此类似(缩短只是为了说明一点): DAL 存储库接口 public interface IRepository { IList GetAll(); TEntity Get(TKey id); TEntity Add(TEntity item); TEntity Update(TEntity item); bool Remove(TKey id); } 基本EF存储库 public class BaseEFRepository : IRepository where TEntity: class, IEntity where TKey: struct { protected readonly DbContext _dbContext; public BaseRepository() { _dbContext = new MyDB(); _dbContext.Configuration.ProxyCreationEnabled = false; _dbContext.Configuration.LazyLoadingEnabled = false; } public […]

调整tiff并保持透明度和c#

我正在尝试调整RGB 8位Tif的大小并保持它在c#中的透明度。 我试过以下代码。 using (Image thumbnail = new Bitmap(1500, 1500)) { using (Bitmap source = new Bitmap(@”c:\trans.tif”)) { using (Graphics g = Graphics.FromImage(thumbnail)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.Clear(Color.Transparent); g.DrawImage(source, 0, 0, 1500, 1500); } } thumbnail.Save(@”c:\testoutput.tif”, ImageFormat.Tiff); //using (MemoryStream ms = new MemoryStream()) //{ // //thumbnail.Save(ms, ImageFormat.Tiff); // // // //result = […]

访问重写方法

有一个练习“OverloadResolutionOverride” 以下代码的输出结果如何: class Foo { public virtual void Quux(int a) { Console.WriteLine(“Foo.Quux(int)”); } } class Bar : Foo { public override void Quux(int a) { Console.WriteLine(“Bar.Quux(int)”); } public void Quux(object a) { Console.WriteLine(“Bar.Quux(object)”); } } class Baz : Bar { public override void Quux(int a) { Console.WriteLine(“Baz.Quux(int)”); } public void Quux(params T[] a) { Console.WriteLine(“Baz.Quux(params […]

为什么我不能将常量字符串的串联分配给常量字符串?

有时我想分解一个常量字符串,原因是格式化,通常是SQL。 const string SELECT_SQL = “SELECT Field1, Field2, Field3 FROM TABLE1 WHERE Field4 = ?”; 至 const string SELECT_SQL = “SELECT Field1, Field2, Field3 ” + “FROM TABLE1 ” + “WHERE Field4 = ?”; 但是,C#编译器不允许第二种forms为常量字符串。 为什么?

解析文本文件并删除双引号内的逗号

我有一个文本文件需要转换为csv文件。 我的计划是: 逐行解析文件 用空格搜索和替换双引号内的逗号 然后删除所有双引号 将该行附加到新的csv文件 问题:我需要一个能识别双引号内的逗号并替换它的函数。 这是一个示例行: “MRS Brown”,“4611 BEAUMONT ST”,“”,“WARRIOR RUN,PA”

如何通过FindWindow api调用在C#中开发的窗口

我是C#和xaml代码的新手。 我有一个用C#实现的示例代码。 当我查看xaml文件时,我得到了 。 test.MainWindow表示此窗口的类名。 我试图从win 32中开发的其他应用程序调用此窗口。我试图将此类名称传递给FindWindow(“test.MainWindow”,NULL) ,但它失败了。 在那边做了什么。 如何更改C#中开发的窗口的类名? 谢谢, 萨加尔

如何向服务器发送“hello”并回复“hi”?

使用我的代码,我可以在服务器上读取消息并从客户端写入。 但我无法从服务器写入响应并在客户端中读取。 client上的代码 var cli = new TcpClient(); cli.Connect(“127.0.0.1”, 6800); string data = String.Empty; using (var ns = cli.GetStream()) { using (var sw = new StreamWriter(ns)) { sw.Write(“Hello”); sw.Flush(); //using (var sr = new StreamReader(ns)) //{ // data = sr.ReadToEnd(); //} } } cli.Close(); server上的代码 tcpListener = new TcpListener(IPAddress.Any, port); tcpListener.Start(); while (run) { var […]

将按钮控件嵌入到现有的Direct3D应用程序中

我想在Direct3D v9游戏(由第三方制作)上面叠加自己的内容。 叠加交互式按钮 具体来说,我想覆盖一个可点击的按钮控件,比如Steam,例如,虽然我正在尝试一个更简单的界面。 理想情况下,我可以覆盖WPF按钮或Windows窗体按钮或整个UserControl,但如果不可能,那么从基元创建一个function按钮就足够了。 文本叠加使用SharpDX 我有一个基于Justin Stenning工作重叠文本的工作样本 即在SharpDX.Direct3D9.Font DrawText方法的帮助下 如何使用SharpDX覆盖Button或UserControl 我查看了https://github.com/sharpdx/SharpDX-Samples中的相关示例,但无法找到使用SharpDX包含或绘制本机控件的方法 可能吗? 如果没有,是否有从头开始绘制可点击按钮的样本? 请记住我是DirectX世界的新手:)