【c# – 用于使用起始值声明属性的简单语法】教程文章相关的互联网学习教程文章

c# – 将一个对象属性值转移到另一个对象属性值【代码】

最重要的是,我知道AutoMapper,我不想使用它.因为我正在学习C#而我想深入了解它.所以我想尝试自己做这个问题(下面解释). 但是,我正在尝试创建一个属性复制器来处理一种类型属性的值到另一种属性,如果该属性具有相同的名称和类型,并且可以从源中读取并在目标中可写.我正在使用type.GetProperties()方法.采样方法如下:static void Transfer(object source, object target) {var sourceType = source.GetType();var targetType = targ...

c# – 查找基础实体的属性使用情况【代码】

我试图在属性属于基类的类中找到属性的用法.这是一个标记示例:class Program {class Item{public DateTime DeletedStamp { get; set; }public decimal Price { get; set; }}class Book : Item{public string Title { get; set; }public string Author { get; set; }}class Bicycle : Item{public string Type { get; set; }public string Producer { get; set; }}static void Main(string[] args){var book = new Book(){Title = ...

c# – 使用Simple Injector进行方法级别的属性拦截【代码】

使用Unity,我可以快速添加基于属性的拦截public sealed class MyCacheAttribute : HandlerAttribute, ICallHandler {public override ICallHandler CreateHandler(IUnityContainer container){return this;}public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext){// grab from cache if I have it, otherwise call the intended method call..} }然后我这样注册Unity:container.RegisterType<IP...

ExpectedException属性无法显示预期结果C#【代码】

我创建了一个类库. 我已经创建了单元测试项目来对单元进行单元测试. 我经历了一个特殊的课程" ExpectedExceptionAttribute Class "我试图实现它.但是,如果我启用该属性,我的代码始终显示失败.即使数据是正确的. 课程:public class SampleTestClass {public double CheckValidAmount(object Name , double amount){try{if (amount == 1.0 && Name.ToString() == "RamKumar")return 10.0 - amount;elsereturn amount;}catch (NullRe...

c# – Entity Framework直接更新虚拟属性而不创建新记录【代码】

这是一个简单的实体:public class Customer : Entity {public virtual Location Location { get; set; } }现在假设我们已经有了一个客户:var customer = new Customer() {Location = new Location("China")};现在我们要更新他的位置:var customer = context.Customers.First(x => x.Location.Country == "China"); customer.Location = new Location("America"); context.SaveChanges();现在,当我查看数据库时,位置记录“中国”...

c# – 按自定义属性设置特定属性的值【代码】

我目前正在开发一种软??件,用户将无法访问该软件的后端,但仍应能够轻松更改应用程序的配置/设置.我决定最好的方法是位于最终构建的根目录中的自定义“配置文件(.cfg)”..cfg文件的简单示例:serveraddress='10.10.10.10' serverport='1234' servertimeout='15000'由于我希望配置文件能够轻松扩展,我决定使用一些自定义属性和一些简单的LINQ.这确实像我期望的那样工作,但由于我仍然是.net中的新手,我担心我没有采用最佳方法,我的问题...

c# – “在GlassCast(分支)模板项目时,您无法保存不包含表示项目ID的属性的类”,为什么?【代码】

我整天都在反对这一切,而且我已经没有想法了.基本上是因为glass.mapper似乎不支持分支模板我正在使用标准DB方法创建一个sitecore项.然后我使用GlassCast< T>()给我一个正确输入的对象. 这个类型的对象似乎包含一个id字段但是,当我尝试保存对象时,我得到错误:You cannot save a class that does not contain a property thatrepresents the item ID这是我的方法:private Glass.Mapper.Sc.ISitecoreService SitecoreService;public...

c# – 有条件地序列化/反序列化属性【代码】

我有一个类,其对象必须根据bool值序列化/反序列化属性[System.SerializableAttribute()] public class Foo {private string myField;private bool myFieldSerializes;//Parameterless construction for serializing purposespublic Foo() { }public Foo(string myField, bool myFieldSerializes){this.myField = myField;this.myFieldSerializes = myFieldSerializes;}public string MyField{get {return this.myField;}set {this....

c# – 如何在Automapper映射中保留源属性的原始值?【代码】

输入视图模型:public class FacilityInputModel {public int Id { get; set; }public string Name { get; set; } }域名模型:public class FacilityInputModel {public int Id { get; set; }public string Name { get; set; }public string OriginalName { get; set; } }我允许用户更改设施的名称,但仍保留其原始名称. 说设施是(为了方便,我正在写json){id:1, name='Facility1', originalName='Facility1'}什么时候创建 我将通过...

c# – 使用集合初始值设定项初始化WebControl对象的Attributes属性【代码】

我想初始化WebControl对象,内联,但对于某些字段,这有点棘手.例如,当我尝试初始化TextBox对象的Attributes属性时,如下所示:using System.Web.UI.WebControls; Panel panel = new Panel() { Controls = { new TextBox() { Attributes = { { "key", "value" } } } } };我收到错误:Cannot initialize type ‘07001’ with a collectioninitializer because it does not implement‘System.Collections.IEnumerable’知道在这种情况下...

c# – 模拟一个只读索引器属性【代码】

我想知道如何模拟索引属性,并且有很多问题: > Moq an indexed property and use the index value in the return/callback> How to MOQ an Indexed property> How to Moq Setting an Indexed property 等.但就我而言,增加了复杂性.索引属性是只读的.所以,我需要能够测试一段执行以下操作的代码if (workbook.Worksheets.Cast<IWorksheet>().Any(ws => ws.Name.CompareNoCase(Keywords.Master))) {... }我们有以下类结构public inter...

处理C#中属性更改的更好方法【代码】

我正在构建一个MVVM应用程序,其中ToBeListened类有几个属性,PropertyA和PropertyB,我想听它们.public class ToBeListened : INotifyPropertyChanged {private double _propertyA;private string _propertyB;/*Here I'm omitting the update part where NotifyPropertyChanged gets called*/public double PropertyA{get; set; }public double PropertyB{get; set; }public event PropertyChangedEventHandler PropertyChanged;prot...

c# – Json.Net:将属性序列化/反序列化为值,而不是对象【代码】

在另一个类中使用时,如何实现Id类的以下JSON表示?class Car {public StringId Id { get; set; }public string Name { get; set; } }class StringId {public string Value { get; set; } }// ---------------------------------------------// Desired representation { "Id": "someId", "Name": "Ford" }// Default (undesired) representation { "Id" : { "Value": "someId" }, "Name": "Ford" }解决方法:您可以为StringId添加Ty...

c# – FileSystemWatcher – ReadOnly属性的更改事件【代码】

当受监视目录中的文件的ReadOnly属性发生更改时,看起来FileSystemMonitor未触发“已更改”事件(并且没有其他事件). 这是我的测试代码:using System;using System.IO;namespace FSM{class Program{static FileSystemWatcher FolderMonitor;static void Main(string[] args){FolderMonitor = new FileSystemWatcher("C:\\MyImages");FolderMonitor.IncludeSubdirectories = false;FolderMonitor.Changed += FolderMonitor_Changed; ...

c# – Linq to XML撤回存在于不同级别的属性【代码】

我正在尝试编写一个Linq to XML查询,该查询返回关键字并包含一个属性(mediatype) 考虑以下XML数据……<?xml version="1.0" encoding="utf-8"?> <media><photos> <photo mediatype="photo" photographer="Jag_cz" description="Colored splashes in abstract shape, isolated on white background" id="16" name="50623755_F.jpg" folder="HR Headset"><keywords><keyword>fish</keyword><keyword>abstract</keyword> ...