如何向生成的类(Web 服务代理)的属性添加属性?
我有一个 Silverlight 3 应用程序,它从 Web 服务获取数据。 当我添加对 Web 服务的引用时,该 Web 服务使用的所有类都在代理类中定义。 到目前为止,一切顺利...现在,我想使用 Silverlight 3 中提供的 DataForm。要使用该组件,我必须将元数据添加到我的类中(例如,请参阅下面的代码)。
public class Movie : IEditableObject
{
public int MovieID { get; set; }
[Display(Name="The Movie Name")]
public string Name { get; set; }
}
我的问题是,考虑到Movie类是在.NET生成的代理类中定义的,如何在不修改生成的类的情况下将属性/元数据添加到Movie类的属性中?
谢谢
I have a Silverlight 3 application which gets data from a web service. When I add a reference to my web service, all the classes used by the web service are defined in a proxy class. So far, so good... Now, I would like to use the DataForm offered in Silverlight 3. To use that component, I must add metadata to my class (e.g. see code below).
public class Movie : IEditableObject
{
public int MovieID { get; set; }
[Display(Name="The Movie Name")]
public string Name { get; set; }
}
My question is, considering the class Movie is defined in the proxy class generated by .NET, how do I add the attributes/metadeta to the properties of the Movie class without modifying the class generated?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果可以将您的 Web 服务转换为使用 .NET RIA 服务,您可以使用此技术将属性应用于属性,而无需使用包装类:
http://blogs.msdn.com/brada/archive/2009/07/21/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part- 7-services-based-data-store.aspx
使用 RIA 服务,您可以在服务器端应用属性 - 代码生成器足够智能,可以在生成代码时选取它们并将它们应用到客户端。
If converting your web service to use .NET RIA Services is a possibility you can use this technique to apply attributes to properties without using a wrapper class:
http://blogs.msdn.com/brada/archive/2009/07/21/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-7-services-based-data-store.aspx
With RIA Services you apply the attributes on the server side - the code generator is intelligent enough to pick them up and apply them on the client side as well when it generates code.
如果您确实需要向生成的类添加某些内容,您仍然可以使用分部类来实现,而无需修改生成的类中的任何内容。
If in case you really need to add something to the generated class, you can still use the partial class to achieve without modifying anything within the generated class.
简而言之; 你不知道。 不要编辑生成的代码,因为您的更改将在下次生成时丢失。
相反,您可以在应用程序中创建您自己的
Movie
类,并创建将生成的Movie
代理类转换为您的内部Movie
类(以及反之)的方法,然后使您的内部类具有所需的行为。 我通常将其包装在较低的级别,以便我的大部分代码永远不会看到生成的代理类。In short; you don't. Don't edit the generated code, since your changes will be lost the next time it is generated.
Instead, create your own
Movie
class inside your application and create methods that translates the generatedMovie
proxy class to your internalMovie
class (and back), and then make your internal class have the desired behavior. I usually wrap this together at a low level, so that most of my code never see the generated proxy classes.