Tag: C#的

C ++静态多态(CRTP)并使用派生类的typedef

我阅读了维基百科有关在C ++中使用静态(读:编译时)多态性的好奇的循环模板模式的文章 。 我想概括它,以便我可以根据派生types更改函数的返回types。 (这看起来应该是可能的,因为基types从模板参数中知道派生types)。 不幸的是,下面的代码不会使用MSVC 2010进行编译(我现在还没有轻松访问gcc,所以我还没有尝试过)。 任何人知道为什么 template <typename derived_t> class base { public: typedef typename derived_t::value_type value_type; value_type foo() { return static_cast<derived_t*>(this)->foo(); } }; template <typename T> class derived : public base<derived<T> > { public: typedef T value_type; value_type foo() { return T(); //return some T object (assumes T is default constructable) } […]

访问UI(主)在WPF中安全地线程

我有一个应用程序更新我的数据网格,每当我正在看日志文件更新(附加新文本)在以下方式: private void DGAddRow(string name, FunctionType ft) { ASCIIEncoding ascii = new ASCIIEncoding(); CommDGDataSource ds = new CommDGDataSource(); int position = 0; string[] data_split = ft.Data.Split(' '); foreach (AttributeType at in ft.Types) { if (at.IsAddress) { ds.Source = HexString2Ascii(data_split[position]); ds.Destination = HexString2Ascii(data_split[position+1]); break; } else { position += at.Size; } } ds.Protocol = name; ds.Number […]

'使用'语句与'最后尝试'

我有一堆属性,我将使用读/写锁。 我可以通过try finally或try finally子句来实现它们。 在try finally的try finally我会在try之前获得locking,并在finally释放。 在using子句中,我将创build一个在其构造函数中获取锁的类,并在其Dispose方法中释放它。 我在很多地方使用读/写锁,所以我一直在寻找比try finally更简洁的方法。 我有兴趣听到一些关于为什么不推荐一种方式或为什么可能比另一种更好的想法。 方法1( try finally ): static ReaderWriterLock rwlMyLock_m = new ReaderWriterLock(); private DateTime dtMyDateTime_m public DateTime MyDateTime { get { rwlMyLock_m .AcquireReaderLock(0); try { return dtMyDateTime_m } finally { rwlMyLock_m .ReleaseReaderLock(); } } set { rwlMyLock_m .AcquireWriterLock(0); try { dtMyDateTime_m = value; } finally { […]

在C#中的C ++联盟

我正在将用C ++编写的库翻译成C#,而关键字'union'只存在一次。 在一个结构中。 把它翻译成C#的正确方法是什么? 它有什么作用? 它看起来像这样; struct Foo { float bar; union { int killroy; float fubar; } as; }

将dd / mm / yyyy格式的string转换为Datetime

我是DotNet和C#的新手。 我想将mm/dd/yyyy格式的string转换为DateTime对象。 我尝试了像下面的parsing函数,但它是抛出一个运行时错误。 DateTime dt=DateTime.Parse("24/01/2013"); 关于如何将其转换为date时间的任何想法?

传递数组在C ++中的function

#include <iostream> using namespace std; void printarray (int arg[], int length) { for (int n = 0; n < length; n++) { cout << arg[n] << " "; cout << "\n"; } } int main () { int firstarray[] = {5, 10, 15}; int secondarray[] = {2, 4, 6, 8, 10}; printarray(firstarray, 3); printarray(secondarray, 5); […]

用Moq嘲笑扩展方法

我有一个预先存在的接口… public interface ISomeInterface { void SomeMethod(); } 我已经扩展了这个内置使用混合… public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } 我有一个这样的调用这个我想testing的类… public class Caller { private readonly ISomeInterface someInterface; public Caller(ISomeInterface someInterface) { this.someInterface = someInterface; } public void Main() { someInterface.AnotherMethod(); } } 和一个testing,我想嘲笑接口,并validation扩展方法的调用… [Test] public void Main_BasicCall_CallsAnotherMethod() […]

如何在C#应用程序中调用VBScript文件?

我需要在我的C#Windows应用程序中调用VBScript文件(.vbs文件扩展名)。 我该怎么做? 在Visual Studio中有一个加载项来访问VBScript文件。 但是我需要在后面的代码中访问脚本。 如何做到这一点?

如何使用RSA和SHA256与.NET签署一个文件?

我的应用程序将获取一组文件并签名。 (我不想签署一个程序集。)有一个.p12文件,我从中获取私钥。 这是我试图使用的代码,但我得到一个System.Security.Cryptography.CryptographicException "Invalid algorithm specified." 。 X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password"); RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey; string id = CryptoConfig.MapNameToOID("SHA256"); return csp.SignData(File.ReadAllBytes(filePath), id); 根据这个答案它不能做( RSACryptoServiceProvider不支持SHA-256),但我希望可能使用不同的库,如Bouncy城​​堡。 我是新来的这个东西,我发现Bouncy Castle很混乱。 我将一个Java应用程序移植到C#,并且我必须使用相同types的encryption来签署这些文件,所以我被卡在RSA + SHA256中。 我如何使用Bouncy Castle,OpenSSL.NET,Security.Cryptography或其他第三方库,我还没有听说过? 我假设,如果可以在Java中完成,那么它可以在C#中完成。 更新: 这是我从poupou的anwser链接中得到的 X509Certificate2 cert = new X509Certificate2(KeyStoreFile, password"); RSACryptoServiceProvider rsacsp = (RSACryptoServiceProvider)cert.PrivateKey; CspParameters cspParam = new CspParameters(); cspParam.KeyContainerName = rsacsp.CspKeyContainerInfo.KeyContainerName; cspParam.KeyNumber […]

如何获得浮点数的符号,尾数和指数

我有一个程序,它运行在两个处理器上,其中一个没有浮点支持。 所以,我需要在该处理器中使用固定点执行浮点计算。 为此,我将使用浮点模拟库。 我需要首先提取处理器上浮点数的符号,尾数和指数。 所以,我的问题是如何获得单精度浮点数的符号,尾数和指数。 按照这个图的格式, 这是我迄今为止所做的,但除了符号,尾数和指数都是正确的。 我想,我错过了一些东西。 void getSME( int& s, int& m, int& e, float number ) { unsigned int* ptr = (unsigned int*)&number; s = *ptr >> 31; e = *ptr & 0x7f800000; e >>= 23; m = *ptr & 0x007fffff; }