当前位置: 首页 > 编程语言 > C#

在webapi中使用OData来获取仅在运行时知道的属性分享

时间:2023-04-11 03:12:45 C#

C#学习教程:在Webapi中使用OData获取仅在运行时已知的属性使用.NETC#webapi控制器的集合:publicclassImage{//////获取图像的名称。///公共字符串名称{get;放;}publicintId{得到;放;}internalSystem.IO.StreamGetProperty(stringp){thrownewSystem.NotImplementedException();}privateDictionarypropBag=newDictionary();内部字符串GetIt(字符串p){returnpropBag[p];在我的WebApiConfig.cs中,我执行标准配置:ODataModelBuildermodelBuilder=newODataConventionModelBuilder();varimagesES=modelBuilder.EntitySet("图像");根据Excel,这是一个很好的提要。但在我的收藏中,propBag包含其他数据的有限列表(例如“a”、“b”和“c”或类似数据)。我希望它们成为我的OData源中的额外属性。我的第一个想法是在配置发生时尝试这样的事情:ODataModelBuildermodelBuilder=newODataConventionModelBuilder();varimagesES=modelBuilder.EntitySet("图像");images.EntityType.Property(c=>c.GetIt("a"))这完全失败了,因为它实际上是传入的表达式树,而不是lambda函数,并且该方法尝试对其进行解析。并期望该属性取消引用。我应该朝这个方向去哪里?对于某些上下文:我正在尝试使用简单的平面对象创建OData只读源。使用网络上的教程非常容易使用简化版本。更新:下面的cellik向我指出了一个方向。我尽可能地跟着它,我已经非常接近了。首先,我创建了一个属性信息类来表示动态属性:publicclassLookupInfoProperty:PropertyInfo{privateImage_image;私有字符串_propName;publicLookupInfoProperty(stringpname){_propName=pname;}publicoverridePropertyAttributesAttributes{get{thrownewNotImplementedException();}}publicoverrideboolCanRead{get{returntrue;}}publicoverrideboolCanWrite{get{returnfalse;}}publicoverrideMethodInfo[]GetAccessors(boolnonPublic){thrownewNotImplementedException();}publicoverrideMethodInfoGetGetMethod(boolnonPublic){thrownewNotImplementedException();}publicoverrideParameterInfo[]GetIndexParameters(){thrownewNotImplementedException();}publicoverrideMethodInfoGetSetMethod(boolnonPublic){thrownewNotImplementedException();}publicoverrideobjectGetValue(objectobj,BindingFlagsinvokeAttr,Binderbinder,object[]index,System.Globalization.CultureInfoculture){thrownewNotImplementedException();}publicoverrideTypePropertyType{get{returntypeof(string);}}publicoverridevoidSetValue(objectobj,objectvalue,BindingFlagsinvokeAttr,Binderbinder,object[]index,System.Globalization.CultureInfoculture){thrownewNotImplementedException();}publicoverrideTypeDeclaringType{get{thrownewNotImplementedException();}}publicoverrideobject[]GetCustomAttributes(TypeattributeType,boolinherit){thrownewNotImplementedException();}publicoverrideobject[]GetCustomAttributes(boolinherit){returnnewobject[0];}publicoverrideboolIsDefined(TypeattributeType,boolinherit){thrownewNotImplementedException();}publicoverridestringName{get{return_propName;}}publicoverrideTypeReflectedType{get{returntypeof(Image);}}}如您所见,很少需要现实这些方法然后我创建了一个自定义顺序列表化器:这);}返回base.CreateEdmTypeSerializer(edmType);}}publicclassCustomEntityTypeSerializer:ODataEntityTypeSerializer{publicCustomEntityTypeSerializer(IEdmEntityTypeReferenceedmType,ODataSerializerProviderserializerProvider):base(edmType,serializerProvider){}//////如果我们正在查看正确的类型,请尝试先进行道具包查找。////////////publicoverrideODataPropertyCreateStructuralProperty(IEdmStructuralPropertystructuralProperty,EntityInstanceContextentityInstanceContext){如果((structuralProperty.DeclaringTypeasIEdmEntityType).Name=="Image"){varr=(entityInstanceContext.EntityInstanceas图片).GetIt(s结构属性.名称);if(r!=null)returnnewODataProperty(){Name=structuralProperty.Name,Value=r};}returnbase.CreateStructuralProperty(structuralProperty,entityInstanceContext);在我的WebApiConfigRegister方法中配置:config.Formatters.InsertRange(0,ODataMediaTypeFormatters.Create(newCustomSerializerProvider(),newDefaultODataDeserializerProvider()));最后,我创建了Image类并向其添加了“a”属性:ODataModelBuildermodelBuilder=newODataConventionModelBuilder();varimagesES=modelBuilder.EntitySet("图像");variST=modelBuilder.StructuralTypes.Where(t=>t.Name=="Image").FirstOrDefault();isT.AddProperty(newLookupInfoProperty("a"));Microsoft.Data.Edm.IEdmModel模型=modelBuilder.GetEdmModel();config.Routes.MapODataRoute("ODataRoute","odata",model);只有一个问题——在大多数测试查询中,EntityInstance为null事实上,它是一个贬值的属性——你将使用EdmObject。这确实有对实际对象实例的引用。但是,在当前的夜间构建中(您必须进行任何此类工作),对EdmObject的访问是内部的-因此无法使用。更新2:在aspCodePlex站点上有一些关于此的最少文档。很接近!不是解决问题的方法,但希望这会有所帮助。这是我们积压工作的主要特点之一。在提及它时,我们倾向于在我们的团队内部将其称为“无类型支持”。WebAPI的问题在于它需要为服务公开的每个EDM类型提供一个可靠的CLR类型。此外,CLR类型和EDM类型之间的映射是一对一的且不可配置的。这也是大多数IQueryable实现的工作方式。notypesupport的想法就是打破这个要求,为不支持强CLR类型的EDM类型提供支持。例如,所有EDM实体都可以由键值字典支持。关于如何在WebAPIOdata中完成序列化的扩展点这是一个示例。自定义asp.netwebapi的odata输出虽然问题不同,但我认为您可以使用相同的方法来完成您想要的(即覆盖条目的序列化方式)。特别是,在覆盖的CreateEntry中,您可以使用entry.Properties可以更改(请注意,此版本尚未发布AFAIK,但可以作为预发布版本下载。)以上是关于C#学习教程:在webapi中使用OData获取仅在运行时共享的属性,如果对大家有用,需要了解更多的C#学习教程。希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: