utilizzando i valori di ritorno da un C # .NET fece costruire come componente COM +
-
15-10-2019 - |
Domanda
finora ho fatto un componente in C # .NET 4 e utilizzare System.EnterpriseServices per renderlo visibile COM. Voglio sviluppare metodi commerciali in C #, ma ho ancora bisogno di loro l'accesso dal classico ASP (VBScript).
Fin qui tutto bene, tutto funziona bene (eccetto l'overloading di funzioni :)). Ora ho fatto una classe di test per ottenere più expirience con codice di ritorno.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.EnterpriseServices;
using System.Management;
namespace iController
{
/// <summary>
/// The tools class provides additional functions for general use in out of context to other classes of the iController.
/// </summary>
public class tools :ServicedComponent
{
#region publich methods
public bool TestBoolean()
{
return true;
}
public string TestString()
{
return "this is a string";
}
public int TestInteger()
{
return 32;
}
public double TestDouble()
{
return 32.32;
}
public float TestFloat()
{
float ret = 2 ^ 16;
return ret;
}
public string[] TestArray()
{
string[] ret = {"0","1"};
return ret;
}
public int[][] TestJaggedArray()
{
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
return jaggedArray;
}
public Dictionary<string, string> TestDictionary()
{
Dictionary<string, string> ret = new Dictionary<string,string>();
ret.Add("test1","val1");
ret.Add("test2","val2");
return ret;
}
#endregion
}
}
Poi ho appena fatto un semplice file di VBScript per eseguirlo con cscript.exe per testare porpuse.
Dim oTools : Set oTools = CreateObject("iController.tools")
WScript.StdOut.WriteLine TypeName(oTools.TestBoolean()) & " - " & oTools.TestBoolean()
WScript.StdOut.WriteLine TypeName(oTools.TestString()) & " - " & oTools.TestString()
WScript.StdOut.WriteLine TypeName(oTools.TestInteger()) & " - " & oTools.TestInteger()
WScript.StdOut.WriteLine TypeName(oTools.TestDouble()) & " - " & oTools.TestDouble()
WScript.StdOut.WriteLine TypeName(oTools.TestFloat()) & " - " & oTools.TestFloat()
test = oTools.TestArray()
WScript.StdOut.WriteLine TypeName(test)
WScript.StdOut.WriteLine UBound(test)
For i = 0 To UBound(test)
WScript.StdOut.WriteLine test(i)
Next
For Each item IN test
WScript.StdOut.WriteLine item
Next
test = oTools.TestJaggedArray()
WScript.StdOut.WriteLine TypeName(test)
For Each item IN test
WScript.StdOut.WriteLine test & " - " & test.Item(item)
Next
test = oTools.TestDictionary()
WScript.StdOut.WriteLine TypeName(test)
For Each item IN test
WScript.StdOut.WriteLine test & " - " & test.Item(item)
Next
Quello che funziona bene:
string, int, foat, double
Quando si tratta di array, o jaggedarray dizionari ho un tipo non corrispondente. VarType è 13 oggetto per il dizionario es ma questo dict sembra essere diversa, allora lo Scripting.Dictionary.
Ho controllato codeproject.com e StackOverflow tutto il giorno e non ho trovato alcun suggerimento exept del filo su StackOverflow dove qualcuno ha menzionato v'è la necessità per creata un'interfaccia IDispatch.
Quindi, chiunque abbia mai avuto lo stesso problema e mi può aiutare o darmi qualche suggerimento che posso andare avanti con?
Soluzione
-
Lezione: lasciate .NET gestire le vostre MarshalAs:)
public object[] Read() { var retVal = new object[] {1,2,3}; return retVal; }
Questo è un array, che io possa accedere da VBScript. L'indizio è che si deve essere oggetto [].
Ora vado sul lavoro su una soluzione per gli array frastagliati e dizionari ...
Altri suggerimenti
Passaggio di matrici a COM: il tutto gestito tipi di matrice possono essere passati a non gestito codice dal codice gestito. A seconda di il tipo gestito e gli attributi applicato ad esso, la matrice può essere si accede come un array di sicurezza o di uno stile di C array
Date un'occhiata a questo articolo: http://msdn.microsoft.com/en- us / library / z6cfh6e6 (v = vs.80) aspx