简体   繁体   中英

How to get constructor from server on client side in C#?

On my client side, I want the default C# constructors from my WCF service C# classes. There reason we want to do this is to have the default values from the server side classes on the client side. From the searches I've done, there are 2 ways to go about this:

  1. When I need a default instance of the object I make a server call that returns me the default instance
  2. I create a client side C# class which inherits from the server side C# class and copy paste the constructor and use this constructor

Is either one of these the "better" option? or is this heavily dependent on how often I would need a new instance of the object?

EDIT: We tried using [System.Runtime.Serialization.OnDeserialized] seen here but it's not working. We are also not using the DataContract attribute in our class, we use ServiceKnownType instead

I think you can try using OnDeserializing attribute

[System.Runtime.Serialization.OnDeserializing]
private void OnDeserializing(StreamingContext ctx)
{
   // Here set your default values
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM