简体   繁体   English

通用方法,通用类型,通用参数

[英]Generic Method, Generic Type, Generic Parameter

I'm not even sure how to describe what I'm trying to do (sorry, newb), but duplicating code because I haven't figured out how to do correctly isn't high on my list. 我什至不知道如何描述我要做什么(对不起,newb),但是由于我没有弄清楚如何正确做,所以重复代码并不在我的清单上。 Any help, please? 有什么帮助吗?

Original non-generic method: 原始非泛型方法:

    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);
    }

My attempt at generic, so close, lost at setting typed property (?): 我对通用的尝试如此接近,却在设置类型属性(?)时迷失了:

    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);
    }

Update: 更新:

Sorry, this all may just be an architecture thing, but there's around 166 possible combinations and it just seems ridiculous to code this one step for each one. 抱歉,这全部可能只是体系结构问题,但是大约有166种可能的组合,对于每个步骤编码这一步似乎很荒谬。 I may have to do just that though... 我可能只需要这样做...

MS Doc reference to eConnect: http://msdn.microsoft.com/en-us/library/ff623781.aspx 对eConnect的MS Doc参考: http : //msdn.microsoft.com/zh-cn/library/ff623781.aspx

Example code that calls the serialization: 调用序列化的示例代码:

    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 code: 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;
        }
    }

Update 2: 更新2:

Many thanks to both of you. 非常感谢你们俩。 After sleeping on it, I realized the error in my architecture. 睡觉之后,我意识到我的体系结构中的错误。 I have to build the eConnect object either way and I'm already building the sub-type object in the previous method call, so I've back-tracked and moved the typed serialize code into the main calling method. 我必须以任何一种方式构建eConnect对象,并且已经在上一个方法调用中构建了子类型对象,因此我已经回溯并将类型化的序列化代码移动到主调用方法中。

I did try the reflection, and while it did compile and run, for some reason it excepted with an ObjectReference/NullReference despite, as far as I could tell, all the objects being populated. 我确实尝试了反射,尽管确实编译并运行了反射,但出于某种原因,它除了ObjectReference / NullReference之外,尽管据我所知,所有对象均已填充。

Here's how I was using it: 这是我的使用方式:

    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);
    }

The problem here is that the generic type parameter can't control the property name. 这里的问题是通用类型参数无法控制属性名称。 The fact that eConnect has a property called IViewMasterType is entirely coincidental as far as the generic type system is concerned. 就通用类型系统而言, eConnect具有称为IViewMasterType的属性的事实完全是偶然的。

You could have eConnectType<T> with a property public T[] SomePropertyName { get; set; } 您可以将eConnectType<T>的属性eConnectType<T> public T[] SomePropertyName { get; set; } public T[] SomePropertyName { get; set; } public T[] SomePropertyName { get; set; } . public T[] SomePropertyName { get; set; } In other words, the typed property name can't be related to its type. 换句话说,类型化的属性名称不能与其类型相关。 Then you'd do this: 然后,您可以这样做:

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); 
} 

But without seeing more of your code it's hard to tell whether this would help. 但是,如果看不到更多的代码,很难说这是否有帮助。

EDIT 编辑

In light of your update, I would lean towards Francis's suggestion of using reflection. 鉴于您的更新,我倾向于弗朗西斯提出的使用反射的建议。 Reflection is slower, but in my experience it has never been so slow that I actually needed to optimize. 反射速度较慢,但​​以我的经验,它从未如此缓慢,以至于我实际上不需要进行优化。

如果您确实需要使用与通用类型相同的名称访问属性,则必须使用Reflection

typeof(eConnectType).GetProperty(typeof(T).Name).SetValue(eConnect, myMaster, null);

After some careful debugging this "getProperties" kept returning "null" references because the members in this eConnecType Class are "Fields" not properties... Here is the fix... 经过一番仔细的调试后,此“ getProperties”一直返回“空”引用,因为此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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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