I have a class object that passes through a web service (WCF). A class has properties of type String and some custom class types.
How to get the name of a property and property The name of a property that has a custom class type.
I tried to flip using GetProperies () but couldn't. GetFields () gave me some success, if the property type has a type string, I also want to get the properties of the properties of the user type.
Here is my code.
public static string ToClassString(this object value) { if (value == null) return null; var builder = new StringBuilder(); builder.Append(value.GetType().Name + "{ "); foreach (var prop in value.GetType().GetFields( BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty)) { builder.Append("{ "); builder.Append(prop.Name + " , "); switch (prop.FieldType.Namespace) { case "System": builder.Append(prop.GetValue(value) + " }"); break; default: builder.Append(prop.GetValue(value).ToClassString() + " }"); break; } } builder.Append("}"); return builder.ToString(); }
I got the result as
NotifyClass {{UniqueId, 16175} {NodeInfo, NodeInfo {} } {EventType, SAPDELETE}}
Here is the class whose instance I want to convert to a string
[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [System.ServiceModel.MessageContractAttribute(WrapperName="NotifyReq", WrapperNamespace="wrapper:namespace", IsWrapped=true)] public partial class Notify { [System.ServiceModel.MessageBodyMemberAttribute(Namespace="custom:namespace", Order=0)] public int UniqueId; [System.ServiceModel.MessageBodyMemberAttribute(Namespace="custom:namespace", Order=1)] public eDMRMService.NodeInfo NodeInfo; [System.ServiceModel.MessageBodyMemberAttribute(Namespace="custom:namespace", Order=2)] public string EventType; public Notify() { } public Notify(int UniqueId, eDMRMService.NodeInfo NodeInfo, string EventType) { this.UniqueId = UniqueId; this.NodeInfo = NodeInfo; this.EventType = EventType; } }
source share