【c# – AutoFixture使用内部setter创建属性】教程文章相关的互联网学习教程文章

c# – 在Visual Studio Debugger中的自定义顺序中显示属性【代码】

在Visual Studio中,是否可以自定义在调试器中检查时显示属性的顺序? 这是一个类的例子,我真的希望StartDate和EndDate彼此相邻,即使它们按字母顺序分开. 其他调试器选项可以通过像DebuggerDisplayAttribute这样的属性进行自定义,所以我希望DisplayOrder能够存在另一个这样的属性.[DebuggerDisplay("{Name}")] public class Rule {public string Name;public int MaxAge;public DateTime StartDate;public DateTime EndDate; }在理想...

c# – 在实体框架中使用导航属性进行延迟加载的逻辑【代码】

我无法理解延迟加载的工作原理.例如,在以下示例中,我可以访问Where()子句中的学生课程:context.Students.Where(st=>st.Courses.Select(c=>c.CourseName).Contains('Math')).ToList();但是,如果我不使用Include(),以下将无法正常工作并抛出null异常,尽管我没有禁用Lazy Loading:context.Students.Single(s => s.StudentId == 1).Courses.ToList()有人可以解释一下为什么它这样工作吗?解决方法:为了使延迟加载工作,必须做两件事:...

C#Automapper Mapping Dictionary属性【代码】

我正在努力弄清楚如何根据Dictionary对象的键来映射值. 这是我到目前为止:.ForMember(dest => dest.Submitter,opts => opts.MapFrom(src => src.opened_by))注意:src.open_by是一个字典对象.我想按键搜索以获取映射到dest.Submitter的值 附加信息: 这是目标对象:public class Incident {public int Active { get; set; }public string Submitter { get; set; } }这是源对象:Dictionary<string, string> opened_by = new Dicti...

如何从c#显示文件属性对话框安全性选项卡【代码】

这篇文章:How do I display a file’s Properties dialog from C#?描述了如何显示文件的属性对话框,我想知道是否可以使用相同的方法但是将打开的选项卡设置为securety?来自C#代码. 见下图. 先感谢您.解决方法:添加info.lpParameters =“安全”;显示安全选项卡. 或者info.lpParameters =“详细信息”;显示详细信息选项卡. 现在ShowFileProperties方法是:public static bool ShowFileProperties(string Filename){SHELLEXECUTEINF...

c# – 使用属性约束类型 – .NET【代码】

在你离开之前:虽然我的代码是在F#中,但这个问题适用于任何.NET语言. 所以这是我的情况 – 我在F#中有一个简单的ProgramOptions记录,用于保存命令行选项数据.每个字段代表一个不同的选项,它们可以具有默认值,这些默认值用自定义属性标记.type ProgramOptionAttribute(defaultValue: obj) =inherit Attribute()type ProgramOptions ={ [<ProgramOption("render.pdf")>] output: string[<ProgramOption(true)>] printOutput: bool// ...

c# – LINQ查询可以将对象类型属性与where条件中的另一个对象匹配【代码】

我有一个对象BankAccount,我想在LINQ查询中检查匹配. 还有另一个对象LenderAccount,它引用了BankAccount实体. 最后一个对象LoanConnection引用了LenderAccount.我正在寻找具有特定BankAccount的所有连接. 以下是代码:var bankAccount = accountService.GetBankAccountById(1); var lenderCons = lenderConnectionService.List().Where(c => c.LenderAccount.BankAccount == bankAccount).ToList();以上查询似乎有效.但我想在LINQ中...

c# – 在调试器中跳过表达式身体属性【代码】

是否有类似的[DebuggerStepThrough]属性可用于C#中的表达式身体属性? 例如,我想跳过代码public Byte ByteArray => Builder.CreateArray();[DebuggerStepThrough]无法应用于属性. C#团队是否在C#6.0中提供任何其他解决方案?解决方法:DebuggerStepThrough对于表达式身体属性无效,因为:[DebuggerStepThrough] public Byte ByteArray => Builder.CreateArray();不编译.但是这样做:public Byte ByteArray {[DebuggerStepThrough]get...

c# – EF手动附加后解析导航属性【代码】

我正在使用实体框架.我使用以下命令将表示DB中的实体的POCO对象附加到我的dbcontext:var entity = new MyEntity() { ID = 1, AnotherItemID = 10 }; context.Set<T>().Attach(entity);到现在为止还挺好.我可以访问该集并使用我添加的实体.它是在Unchanged状态下添加的.但是,它只是一个POCO而不是代理.因此,当我尝试访问导航属性时,例如myEntity.AnotherItem,我只是得到一个null. 有没有人知道是否有办法让EF解决以这种方式附加的P...

c# – 如何使用DataAnnotation将对象序列化为JSON,以使用ServiceStack请求格式化double属性【代码】

使用ServiceStack我需要格式化这个请求,以便CodValue属性保持这种格式#.#[DataContract(Name = "request1")] public class Request1 {[DataMember(Name = "codValue")]public double CodValue { get; set; } }var request1 = new Request1 { CodValue = 0.0 } _serviceClientBase.Post(request1);但是,当我发送请求时,服务器端收到CodValue = 0但是由于服务器端是Java,它会返回一个错误,说它是Java.Lang.Long而不是Double. 如何强制...

c# – 用于属性注入的DryIOC容器配置【代码】

我已经搜索了一个简单的例子来说明如何配置DryIoc容器以简单地将依赖关系注入属性,就像它注入构造函数args一样. 鉴于以下工作示例…… 集装箱登记:public static void Register(HttpConfiguration config){var c = new Container().WithWebApi(config);c.Register<IWidgetService, WidgetService>(Reuse.Singleton);c.Register<IWidgetRepository, WidgetRepository>(Reuse.Singleton);}小部件服务:public class WidgetService :...

c# – 在Nhibernate QueryOver中使用计算属性(未映射)【代码】

我有一个表A,其中包含以下列: AId – TargetId – IsActive 对应于此表,我有下面的类(带有额外的计算属性)和mapper:public class A {public virtual long AId { get; set; }public virtual int TargetId { get; set; }public virtual int IsActive { get; set; }//calculated property, doesn't exist in the tablepublic virtual bool IsClientSide{get { return ((this.TargetId & TargetEnum.ClientSide) != 0); }} }usi...

c# – 具有非ConfigurationElement属性的自定义配置部分【代码】

我有一个工作的自定义配置部分.但是,通过ConfigurationElementCollection获取我的数据是一件痛苦的事情,但是当我尝试将我的属性实现为IEnumerable时,它会因错误而失败:ConfigurationErrorsException was unhandled “Property‘contacts’ is not a ConfigurationElement.”以下是导致失败的代码:[ConfigurationProperty("contacts", IsDefaultCollection = false)] public IEnumerable<string> Contacts {get { return ((Contac...

c# – 如何使用反射将对象添加到类的实例的泛型列表属性中【代码】

我下面有一个班级结构.我收到了这个错误.我错过了什么吗?Object does not match target type.阶级结构public class Schedule {public Schedule() { Name = ""; StartDate = DateTime.MinValue; LectureList = new List<Lecture>(); }public string Name { get; set; }public DateTime StartDate { get; set; }public List<Lecture> LectureList { get; set; } }public class Lecture {public string Name { get; set; }public int...

c# – 子实体的继承和导航属性【代码】

我在使用继承时遇到了导航属性的问题(TPH – 目前在EF Core中唯一可用的). 我的层次模型:public class Proposal {[Key]public int ProposalId { get; set; }[Required, Column(TypeName = "text")]public string Substantiation { get; set; }[Required]public int CreatorId { get; set; }[ForeignKey("CreatorId")]public Employee Creator { get; set; }}public class ProposalLeave : Proposal {[Required]public DateTime L...

c# – EntityFramework Core Fluent模型生成器键和属性【代码】

好吧,在实体框架6中,我会在一个语句中生成密钥和属性数据库:modelBuilder.Entity<Function>().HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);在实体框架核心(7)中,这不起作用:modelBuilder.Entity<Function>().HasKey(x => x.Id).Property(x => x.Id).ValueGeneratedNever();错误:“’KeyBuilder’不包含’Property’的定义,也没有扩展方法’Property’接受’KeyBuilder’类...