Tag: C#的

使用前面的函数参数声明新函数是否合法?

下面的代码用GCC干净地编译: void func(int arg1, decltype(arg1) arg2) { (void)arg2; } int main(){} 我用这个命令来编译: g++ -std=c++14 test.cpp -o test -pedantic-errors -Wall -Wextra 但是在函数声明中使用这个参数看起来很奇怪。 它实际上在标准C ++中是否有效,还是GCC扩展?

实施Repository模式的最佳方法是什么?

我一直在探索BDD / DDD,并试图提出一个适当的Repository模式的实现。 到目前为止,很难就实现这一目标的最佳方式达成共识。 我试图把它归结为以下变化,但我不确定哪个是最好的方法。 作为参考,我正在用NHibernate构build一个ASP.MVC应用程序作为后端。 public interface IRepository<T> { // 1) Thin facade over LINQ T GetById(int id); void Add(T entity); void Update(T entity); void Remove(T entity); IQueryable<T> Find(); // or possibly even T Get(Expression<Func<T, bool>> query); List<T> Find(Expression<Func<T, bool>> query); } public interface IRepository<T> { // 2) Custom methods for each query T […]

在Automapper中使用configuration文件以不同的逻辑映射相同的types

我在我的ASP.NET MVC网站中使用AutoMapper将我的数据库对象映射到ViewModel对象,我试图使用几个configuration文件来映射相同的types,但使用另一个逻辑。 我有这样的想法,通过阅读马特的博客文章 ,他说: 真正关键的部分是AutoMapperconfiguration文件。 您可以将configuration与configuration文件分组。 也许在一个configuration文件中,您以一种方式格式化date,在另一个configuration文件中,以另一种方式格式化date 我只是在这里使用一个configuration文件。 所以我创build了一个案例的configuration文件: public class MyProfile : Profile { protected override string ProfileName { get { return "MyProfile"; } } protected override void Configure() { CreateMap<DateTime, String>().ConvertUsing<StringFromDateTimeTypeConverter>(); } } public class StringFromDateTimeTypeConverter : ITypeConverter<DateTime, String> { public string Convert(DateTime source) { return source.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture); } } 另一个案例是: public class […]

本地types作为C ++中的模板参数

这是我的代码 #include <vector> template <typename T, template<typename> class C = std::vector > struct FooBar { /*codez*/ }; template<typename T> struct Global{}; int main() { struct Local{}; FooBar<Local,Global> k; } 这是我得到的错误 template argument for 'template<class T, template<class> class C> struct FooBar' uses local type 'main()::Local' 标准的哪一部分说这是错误的? 我正在使用gcc 4.5.1。 如何使这个代码工作?

C中“ifndef”和“if!defined”的区别?

我在同一个C源文件中看到了#ifndef ABC和#if !defined (ABC) 。 他们之间有微妙的区别吗? (如果是风格问题,为什么有人在同一个文件中使用它们)

我应该返回0或1成功的function?

可能重复: 在C代码中处理错误 你应该使用C中的失败函数调用什么样的返回值? 我总是使用0,但是它在if,while等等中不可读 我应该返回1吗? 为什么main函数返回成功?

为什么Thread.CurrentPrincipal需要“等待Task.Yield()”?

下面的代码被添加到新创build的Visual Studio 2012 .NET 4.5 WebAPI项目中。 我试图在asynchronous方法中分配HttpContext.Current.User和Thread.CurrentPrincipal 。 Thread.CurrentPrincipal的分配不正确,除非await Task.Yield(); (或其他任何asynchronous)被执行(传递true到AuthenticateAsync()将导致成功)。 这是为什么? using System.Security.Principal; using System.Threading.Tasks; using System.Web.Http; namespace ExampleWebApi.Controllers { public class ValuesController : ApiController { public async Task GetAsync() { await AuthenticateAsync(false); if (!(User is MyPrincipal)) { throw new System.Exception("User is incorrect type."); } } private static async Task AuthenticateAsync(bool yield) { if […]

为什么在C#中int 是uint == true“

请有人澄清C#关键字请。 特别是这两个问题: Q1)第5行; 为什么这个回报是真的? Q2)第7行; 为什么没有抛出exception? public void Test() { object intArray = new int[] { -100, -200 }; if (intArray is uint[]) //why does this return true? { uint[] uintArray = (uint[])intArray; //why no class cast exception? for (int x = 0; x < uintArray.Length; x++) { Console.Out.WriteLine(uintArray[x]); } } } MSDN的描述没有说明情况。 它表示,如果满足这些条件中的任何一个,将会返回true。 (http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx>MDSN […]

嘲笑任何给定types参数的generics方法调用

我有一个接口 public interface IDataProvider { T GetDataDocument<T>(Guid document) where T:class, new() } 我想嘲笑它,它会返回一个给定types的新实例,而不pipetypes如何: myMock.Setup(m => m.GetDataDocument<It.IsAny<Type>()>(It.IsAny<Guid>())) .Returns(() => new T()); (这当然不起作用,因为我不能只给moq任何types的参数,我不知道哪个types必须返回。 任何想法在这一个?

C / C ++中的types究竟是什么?

C / C ++中的types究竟是什么? 编译器如何检查是否需要明确的types转换(和有效的)? 它是否比较价值所需的空间? 如果我有例如: int a; double b = 15.0; a = (int) b; 如果我没有记错,一个double值需要更多的空间(8个字节?!),而不是一个整数(4个字节)。 而且两者的内在performance是完全不同的(两个/尾数的补充)。 那么内部会发生什么? 这里的例子非常简单,但是在C / C ++中有大量的types转换。 编译器如何知道(或程序员)是否可以将FOO转换为BAR?