我甚至不知道如何描述我想要做的事情(对不起,newb),但是重复代码,因为我还没有弄清楚如何正确地做,在我的清单上并不高。有什么帮助吗?
原始非通用方法:
public static string SerializetaUpdateCreateItemRcd(IVItemMasterType o)
{
eConnectType eConnect = new eConnectType();
IVItemMasterType[] myMaster = { o };
// Populate the eConnectType object with the schema object
eConnect.IVItemMasterType = myMaster;
return MemoryStreamSerializer(eConnect);
}
在设置类型化属性(?)时,我试图设置泛型的、如此接近的、失败的:
public static string Serialize<T>(T o) where T : eConnectType
{
eConnectType eConnect = new eConnectType();
T[] myMaster = { o };
// Populate the eConnectType object with the schema object
eConnect.? = myMaster;
return MemoryStreamSerializer(eConnect);
}
更新:
对不起,这一切可能只是一个架构的问题,但有大约166个可能的组合,这似乎是荒谬的编码这一步为每一步。不过,我可能得这么做.
MS引用eConnect:http://msdn.microsoft.com/en-us/library/ff623781.aspx
调用序列化的示例代码:
IVItemMasterType o = new IVItemMasterType();
o.eConnectProcessInfo = null;
o.taCreateInternetAddresses_Items = null;
o.taCreateItemVendors_Items = null;
o.taCreateKitItemRcd_Items = null;
o.taItemSite_Items = null;
o.taIVCreateItemPriceListHeader = null;
o.taIVCreateItemPriceListLine_Items = null;
o.taRequesterTrxDisabler_Items = null;
o.taUpdateCreateItemCurrencyRcd_Items = null;
o.taUpdateCreateItemRcd = eConnectHelper.taUpdateCreateItemRcdFactory(eItem);
// Serialize into string & add to list
List<string> sList = new List<string>();
sList.Add(eConnectHelper.Serialize(o));
// Submit list to eConnect
eCreateEntity(sList);
SerializeMemoryStream代码:
public static string MemoryStreamSerializer(eConnectType e)
{
XmlSerializer serializer = new XmlSerializer(e.GetType());
using (var memoryStream = new MemoryStream())
{
serializer.Serialize(memoryStream, e);
memoryStream.Position = 0;
// Use memory streamed XML document to create a string representation of the object
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(memoryStream);
memoryStream.Close();
string sDocument = xmldoc.OuterXml;
return sDocument;
}
}
更新2:
非常感谢你们俩。睡过觉后,我意识到了架构中的错误。我必须以任何一种方式构建eConnect对象,而且我已经在前面的方法调用中构建了子类型对象,所以我已经将类型化序列化代码移到主调用方法中。
我确实尝试了反射,尽管它确实编译和运行,但由于某种原因,它例外于ObjectReference/NullReference,尽管据我所知,所有对象都被填充。
我就是这样用它的:
public static string Serialize<T>(T o)
{
eConnectType e = new eConnectType();
T[] myMaster = { o };
// Populate the eConnectType object with the schema object
typeof(eConnectType).GetProperty(typeof(T).Name).SetValue(e, myMaster, null);
return MemoryStreamSerializer(e);
}
发布于 2012-02-22 19:04:15
经过仔细的调试,这个"getProperties“继续返回"null”引用,因为这个eConnecType类中的成员是“eConnecType”而不是属性.这是解决办法..。
public static string Serialize<T>(T o)
{
eConnectType e = new eConnectType();
T[] myMaster = { o };
// Populate the eConnectType object with the schema object
typeof(eConnectType).GetField(typeof(T).Name).SetValue(e, myMaster);
return MemoryStreamSerializer(e);
}
发布于 2011-11-15 19:47:35
这里的问题是泛型类型参数无法控制属性名。就泛型类型系统而言,eConnect
具有一个名为IViewMasterType
的属性,这完全是巧合。
您可以使用属性为eConnectType<T>
的public T[] SomePropertyName { get; set; }
。换句话说,类型化的属性名不能与其类型相关。你就会这么做:
public static string Serialize<T>(T o)
{
eConnectType<T> eConnect = new eConnectType<T>();
T[] myMaster = { o };
// Populate the eConnectType object with the schema object
eConnect.SomePropertyName = myMaster;
return MemoryStreamSerializer(eConnect);
}
但是,如果没有看到更多的代码,就很难判断这是否有用。
编辑
根据你的最新情况,我倾向于弗朗西斯使用反射的建议。反射是缓慢的,但在我的经验中,它从来没有如此缓慢,以至于我实际上需要优化。
发布于 2011-11-15 20:21:28
如果确实需要访问具有相同泛型类型名称的属性,则必须使用反射。
typeof(eConnectType).GetProperty(typeof(T).Name).SetValue(eConnect, myMaster, null);
https://stackoverflow.com/questions/8142045
复制相似问题