草庐IT

ATTRIBUTE

全部标签

c# - 我应该在 C# 中将 XML 生成为字符串吗?

在C#中生成XML时,将其生成为字符串是否有问题?过去,我发现以编程方式生成XML非常冗长和复杂。通过字符串连接/字符串构建器创建xml似乎容易得多,但感觉像是一种不好的做法。我应该将XML生成为字符串吗? 最佳答案 XDocument、XElement和XAttribute类使在C#中生成xml变得更加容易。比使用XmlDocument或XmlWriter。例如,生成这个:你可以这样做:XDocumentxDocument=newXDocument(newXElement("RootElement",newXElement("Ch

c# - 我如何获得 C# 中两个列表的差异?

好的,所以我在C#中有两个列表Listattributes=newList();Listsongs=newList();一个是字符串,一个是我创建的属性对象......非常简单classAttribute{publicstringsize{get;set;}publicstringlink{get;set;}publicstringname{get;set;}publicAttribute(){}publicAttribute(strings,stringl,stringn){size=s;link=l;name=n;}}我现在必须比较属性名称中没有的歌曲,例如songs.Add("s

c# - IFilterProvider 和关注点分离

我有一种情况需要在操作过滤器中注入(inject)一些依赖项,即我的自定义授权属性中的自定义授权提供程序。我偶然发现很多人和帖子都在说我们应该将“属性元数据”与“行为”分开。这是有道理的,还有一个事实是过滤器属性没有通过“DependencyResolver”实例化,因此很难注入(inject)依赖项。所以我对我的代码做了一些重构,我想知道我是否做对了(我使用CaSTLeWindsor作为DI框架)。首先,我剥离了我的属性,只包含我需要的原始数据[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]publicc

c# - "The ' http ://www. w3.org/XML/1998/namespace:lang ' attribute is not declared."

有时,在使用XmlValidatingReader验证某些XML文档时,我收到以下错误:System.Xml.Schema.XmlSchemaValidationException:"The'http://www.w3.org/XML/1998/namespace:lang'attributeisnotdeclared."同一个文档有时会成功。我不明白为什么。我的XSD像这样导入架构:...在XML文档中我有以下属性:最后,XmlReaderSettings:constXmlSchemaValidationFlagsvalidationFlags=XmlSchemaValidation

c# - 属性规范中奇怪的歧义(两个 using 指令)

背景在attributespecification,有时有两种有效的方法来编写应用的属性。例如,如果属性类的名称为HorseAttribute,您可以将该属性应用为[HorseAttribute]或只是[Horse]。歧义可以用@解决,例如[@Horse]。以下是一个有效的程序:usingSystem;usingAlpha;usingBeta;namespaceN{[Horse]classC{}}namespaceAlpha{//validnon-abstractattributetypewithaccessibleconstructorclassHorseAttribute:Attr

c# - C# 'event' 关键字的多重含义?

我最近重新阅读了EricLippert的ridiculouslyawesome上的一些旧帖子博客并遇到thistidbit:AconsiderablefractionofthekeywordsofC#areusedintwoormoreways:fixed,into,partial,out,in,new,delegate,where,using,class,struct,true,false,base,this,event,returnandvoidallhaveatleasttwodifferentmeanings.为了好玩,我的同事和我自问自答,除了其中一个关键字之外,我能够为所有

c# - 何时创建属性对象?

由于属性实际上只是附加到程序集的元数据,这是否意味着属性对象仅根据请求创建(例如当您调用GetCustomAttributes时)?或者它们是在创建对象时创建的?或者,由于CLR的属性扫描而在创建对象时创建前2个的组合? 最佳答案 通过C#从CLR,第三版:Ifyouwanttoconstructanattributeobject,youmustcalleitherGetCustomAttributesorGetCustomAttribute.Everytimeoneofthesemethodsiscalled,itconstruc

c# - 在运行时更改自定义属性的参数

我需要在运行时更改属性的参数。我将问题简化为简单示例。属性类:[AttributeUsage(AttributeTargets.Property)]publicclassMyAttribute:Attribute{publicstringName{get;set;}}简单的实体,用属性装饰属性:publicclassMyEntity{[MyAttribute(Name="OldValue1")]publicstringData1{get;set;}[MyAttribute(Name="OldValue2")]publicstringData2{get;set;}}我创建了MyEntit

c# - "The type or namespace name ' 使用 ' could not be found"路由 "attribute routing"

只是试图将一些代码从一个工作项目拼接到另一个。“from”项目使用“属性路由”,您可以在WebAPIController模块中嵌入[Route(…)]指令来指示应该将哪个HTTP消息路由到哪个服务例程。在“from”项目中工作正常,但在“to”项目中我收到构建错误“找不到类型或namespace名称‘Route’(是否缺少using指令或程序集引用?)"我已经尝试将“from”项目中的所有using语句基本上复制到“to”项目中,但这没有明显效果。没有任何MS文档表明需要NuGet包(甚至是using语句)。这两个项目都应该是ASP.NETMVC4。(是的,我用config.MapHt

c# - 为什么 XElement 没有 GetAttributeValue 方法?

有时我想知道某些API更改的原因。由于谷歌没有帮助我解决这个问题,也许StackOverflow可以。为什么Microsoft选择删除XML元素上的GetAttribute辅助方法?在System.Xml世界中有XmlElement.GetAttribute("x")就像它之前的MSXML中的getAttribute一样,两者都返回属性值或缺失时为空字符串。使用XElement有SetAttributeValue但GetAttributeValue没有实现。当然,修改逻辑以测试和使用XElement.Attribute("x").Value属性并没有太多工作,但它不是那么方便,并且以一