I have the below function.
public static string SerializeObjectToXML<T>(object mainColumn)
{
return string.empty;
}
I am trying to pass classname
based on enum value as below.
var tempstr = ClassName.SerializeObjectToXML<EnumClass.class1 == enumvalue ? ClassA: ClassB>(value);
Can I do this? If not how can I achieve this using inline enum condition?
I would suggest this:
var tempstr = EnumClass.class1 == enumvalue
? ClassName.SerializeObjectToXML<ClassA>(value)
: ClassName.SerializeObjectToXML<ClassB>(value);
I can't help but think you are not using generics to your full advantage. Just change object
in your code to T
. Something like this:
public string SerializeObjectToXml<T>(T obj)
{
var sb = new StringBuilder();
using (var wrt = new StringWriter(sb))
{
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(wrt, obj);
}
return sb.ToString();
}
And then call it like this:
ClassA a = new ClassA();
string objXml = SerializeObjectToXml(a);
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.