简体   繁体   English

如何将经典ASP Scripting.Dictionary传递给C#COM类库?

[英]How to pass a Classic ASP Scripting.Dictionary to a C# COM Class Library?

I am working on a classic ASP application that was handed to me that I have to work with. 我正在处理必须交给我的经典ASP应用程序。 I would like to use a component that is only in the .NET framework. 我想使用仅在.NET框架中的组件。 I need to make a C# class library that can do the .NET code that can be called through the COM on classic ASP. 我需要制作一个可以执行.NET代码的C#类库,该代码可以通过经典ASP上的COM调用。

I would like to pass in a classic ASP dictionary into the C# com component. 我想将经典的ASP词典传递到C#com组件中。 Best case would be to pass a dictionary of dictionaries. 最好的情况是通过字典词典。 I have added Scripting.Dictionary from the com as a reference in my C# code. 我已从com中添加Scripting.Dictionary作为我的C#代码中的参考。

Here is my current code. 这是我当前的代码。

Classic ASP: 经典ASP:

Set test = Server.CreateObject("Reporting") 'This is my custom Object
Set dic = Server.CreateObject("Scripting.Dictionary")
dic.Add "Test", 1
test.GetReport 1, dic

C#: C#:

public byte[] GetReport(int Report_Type_Requested, Scripting.DictionaryClass Report_Params)
        {
            try
            {
                return report.GetReport(Report_Params);
            }
            catch (Exception e)
            {               
                throw e;
            }                       
        }

But when I run this I get: 但是当我运行它时,我得到:

Message = "Unable to cast COM object of type 'System.__ComObject' to class type 'Scripting.DictionaryClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject ...

I've also tried this and it didn't seem to have worked: 我也尝试过,但似乎没有用:

public byte[] GetReport(int Report_Type_Requested, Dictionary Report_Params)
            {
                try
                {
                    return report.GetReport(Report_Params);
                }
                catch (Exception e)
                {               
                    throw e;
                }                       
            }

I thought this one might work but it tells me you can't cast it: 我以为这可能有效,但它告诉我您不能投放它:

public byte[] GetReport(int Report_Type_Requested, object Report_Params)
                {
                    try
                    {
                        return report.GetReport((Scripting.DictionaryClass)Report_Params);
                    }
                    catch (Exception e)
                    {               
                        throw e;
                    }                       
                }

How can I accomplish what I am trying to do? 我该如何完成我想做的事情?

Thank you. 谢谢。

EDIT: 编辑:

I tried using IDictionary like the following at got "Invalid Procedure call or argument": 我尝试在获得“无效的过程调用或参数”的情况下使用如下所示的IDictionary:

 public byte[] GetReport(int Report_Type_Requested, IDictionary Report_Params)
                    {
                        try
                        {
                            return report.GetReport(Report_Params);
                        }
                        catch (Exception e)
                        {               
                            throw e;
                        }                       
                    }

EDIT 2: 编辑2:

I tried using Scripting.IDictionary to specify it better. 我尝试使用Scripting.IDictionary更好地指定它。 I got the same error "Invalid Procedure call or argument": 我收到相同的错误“无效的过程调用或参数”:

public byte[] GetReport(int Report_Type_Requested, Scripting.IDictionary Report_Params)
                        {
                            try
                            {
                                return report.GetReport(Report_Params);
                            }
                            catch (Exception e)
                            {               
                                throw e;
                            }                       
                        }

EDIT 3: 编辑3:

I added the code for Get Report. 我添加了获取报告的代码。 I keep changing the GetReport paramter to whatever I am testing. 我一直将GetReport参数更改为我正在测试的参数。

public byte[] GetReport(Scripting.IDictionary Report_Params)
        {
           return null;
        }

The following should work (you need to reference the "Microsoft Scripting Runtime" COM object of course): 以下应该可以工作(您当然需要引用“ Microsoft Scripting Runtime” COM对象):

    public byte[] GetReport(int Report_Type_Requested, object Report_Params)
    {
        Scripting.Dictionary dic = (Scripting.Dictionary)Report_Params;
        foreach (string key in dic)
        {
            object value = dic.get_Item(key);
        }
        ...
    }

I fixed it with the following: 我用以下方法修复了它:

It seems that when an object gets passed from Classic ASP to a COM object, it MUST be passed as an object, therefore the final definition is: 似乎当一个对象从Classic ASP传递到COM对象时,它必须作为一个对象传递,因此最终定义是:

public byte[] GetReport(int Report_Type_Requested, object Report_Params)

In order to cast that object to the dictionary object I found out about the Marshal class from googling: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal(v=vs.100).ASPX 为了将该对象转换为字典对象,我从谷歌搜索中找到了有关Marshal类的信息: http : //msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal (v= vs.100) .ASPX

Making the line I needed: 进行我需要的行:

Scripting.DictionaryClass Report_Dictionary = (Scripting.DictionaryClass)Marshal.CreateWrapperOfType(Report_Params, typeof(Scripting.DictionaryClass));

I was then able to use the dictionary in my code. 然后,我可以在代码中使用字典。

I was also able to use this technique to bring in ADODB.Recordsets over the COM as well. 我还能够使用此技术通过COM引入ADODB.Recordsets。

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

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