Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Problem passing array to .NET COM
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Divers
Thread ID:
01523745
Message ID:
01523897
Vues:
47
Hi,

>One problem with this is that COM Interop can't access generic types at all. So unless you use helper methods to add items into the lists in the first place you can't manipulate the lists.

There was no intention of exposing generic methods to COM. But since Jon is now using XML then VFP doesn't even deal with concrete .NET types directly - i.e. the Dll would be something like this:
[ComVisible(false)]
    public class MyDll
    {
        Dictionary<string, List<Contact>> lists = new Dictionary<string, List<Contact>>();

        [ComVisible(true)]
        public void AddClientsToList(string xml, string listName)
        {
            List<Contact> c = Helper.RetrieveList<Contact>(xml);
            if (!lists.Keys.Contains(listName))
                lists.Add(listName, c);
            else
                lists[listName] = c;
        }

        [ComVisible(true)]
        public string GetClientsList(string listName)
        {
            return Helper.ConvertToXML(lists[listName]);
        }
    }
>FWIW, I tend to use generic serializers for this (a la SerializationUtils.SerializeObject(inst, instType) and SerializationUtils.DeserializeObject(inst,instType) which works across all serializable types.

I've got a similar class in a WPF project but somewhat simpler since the serialized result always ends up in a SQL table as a byte[].
Biggest problem there was that many WPF types are not natively serializable so I had to write a *lot* of ISerializationSurrogate implementations :-{

>SerializationUtils.cs
>
>+++ Rick ---
>
>>Hi,
>>Sounds OK - but if you don't really need a datatable then if's very simple to use a generic list instead. A couple of generic methods will convert any list of objects to XML and back (assuming the objects themselves are serializable). e.g:
public static class Helper
>>    {
>>        public static List<T> RetrieveList<T>(string s)
>>        {
>>            XmlSerializer xs = new XmlSerializer(typeof(List<T>));
>>            System.Xml.XmlReader v = System.Xml.XmlReader.Create(new StringReader(s));
>>            return (List<T>)xs.Deserialize(v);
>>        }
>>
>>        public static string ConvertToXML<T>(List<T> list)
>>        {
>>            XmlSerializer xs = new XmlSerializer(typeof(List<T>));
>>            StringWriter sw = new StringWriter();
>>            xs.Serialize(sw, list);
>>            return sw.ToString();
>>        }
>>    }
Example use:
       List<Contact> list = new List<Contact>();
>>            list.Add(new Contact { Name = "Fred", Age = 33 });
>>            list.Add(new Contact { Name = "Joe", Age = 66 });
>>
>>            string xmlString = Helper.ConvertToXML<Contact>(list);
>>            //(Examine this string to determine what VFP should create)
>>
>>            List<Contact> retrievedList = Helper.RetrieveList<Contact>(xmlString);
>>>Thanks for the tip. In the meantime, I've decided to switch to sending XML and convert that to a DataTable that I can iterate through. That seems to do the trick for now.
>>>
>>>Jon
>>>
>>>>>My C# COM DLL has a public method with the following signature:
>>>>>
>>>>>public void AddClientsToList(Contact[] contacts, string listName)
>>>>>
>>>>>
>>>>>In VFP, I do the following:
  • Call a DLL method to get a new contact object
  • Populate the properties
  • Create an array and place the object in the first element
  • Call COMARRAY(loDLL,10)

>>>>>When I attempt loDLL.AddClientsToList(@MyArray, "test"), I get a type mismatch COM error.
>>>>>
>>>>>Any ideas on what I'm doing wrong?
>>>>
>>>>No. But passing arrays of anything is always a pain. Since you have control of both sides maybe it would be simpler to just pass the Contacts in one-by-one? e.g:
public class MyDll
>>>>    {
>>>>        Dictionary<string, List<Contact>> lists = new Dictionary<string, List<Contact>>();
>>>>        public Contact GetNewContact() { return new Contact();}
>>>>       
>>>>        public void AddClientToList(Contact contact,string listName)
>>>>        {
>>>>             if (!lists.Keys.Contains(listName))
>>>>                lists.Add(listName, new List<Contact>());
>>>>            lists[listName].Add(contact);
>>>>        }
>>>>    }
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform