en utilisant des valeurs de retour d'un c # .net fait construire le composant comme com +
-
15-10-2019 - |
Question
jusqu'à présent, je fait un composant C # .NET 4 et System.EnterpriseServices d'utilisation pour le rendre visible la sortie COM. Je veux développer des méthodes d'affaires en C #, mais je les ai toujours besoin d'accéder à partir d'ASP classique (de vbscript).
Jusqu'à présent, si bon, tout fonctionne bien (surcharge de fonction exepté :)). Maintenant, je fait une classe de test pour obtenir plus d'expirience avec le code de retour.
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
}
}
Je viens de faire un simple fichier vbscript pour l'exécuter avec cscript.exe pour tester 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
Ce qui fonctionne bien:
string, int, foat, double
En matière de tableau, jaggedarray ou dictionnaires je reçois une incompatibilité de type. VarType est 13 objet pour le dictionnaire, par exemple mais ce dict semble être différent alors le Scripting.Dictionary.
J'ai vérifié codeproject.com et Stackoverflow toute la journée et n'a pas trouvé aucun indice eXept un fil sur stackoverflow où quelqu'un l'a mentionné, il est nécessaire de créer une interface IDispatch.
Alors quelqu'un a déjà eu le même problème et peut me aider ou me donner quelques conseils que je peux aller avec?
La solution
-
Leçon: laissez .NET gérer vos MarshalAs:)
public object[] Read() { var retVal = new object[] {1,2,3}; return retVal; }
est un tableau que je peux accéder à partir vbscript. L'indice est qu'il doit faire l'objet [].
Maintenant, je continue à travailler sur une solution pour les tableaux et les dictionnaires déchiquetés ...
Autres conseils
Passage de tableaux à COM: tous gérés types de tableaux peuvent être transmis à non géré Code de code managé. Cela dépend de le type géré et les attributs appliqué, le tableau peut être accessible comme un tableau sécurisé ou un style C array
Jetez un oeil à cet article: http://msdn.microsoft.com/en- nous / bibliothèque / z6cfh6e6 (v = vs.80) .aspx