传递一个命名空间到一个函数 [英] Passing a namespace into a function

查看:145
本文介绍了传递一个命名空间到一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它接受一个Word文档,并将其保存为HTML格式。我想使用相同的功能与任何文档类型的工作。我已经使用泛型(我assumming不同的文档的API都是一样的),它失败,因为乔恩斯基特指出的原因尝试。有另一种方式?

 使用Word =的Microsoft.Office.Interop.Word;
使用Excel =的Microsoft.Office.Interop.Excel;

//工程确定
私人无效convertDocToHtm(字符串文件名)
{
...略

     VAR应用=新Word.Application();
     VAR DOC =新Word.Document();
     DOC = app.Documents.Open(REF文件名,参考失踪,裁判trueValue,裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,参考失踪,失踪参考);

...略
}

//失败惨淡经营(编译),因为T是一个类型参数,这是不是在给定的范围内有效 - 也就是说Word是一个命名空间不是类
私人无效convertDocToHtm2< T>(字符串文件名)
{
...略

     VAR应用=新T.Application();
     VAR DOC =新T.Document();
     DOC = app.Documents.Open(REF文件名,参考失踪,裁判trueValue,裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,参考失踪,失踪参考);

...略
}

//调用示例
convertDocToHtm(文件名);
convertDocToHtm2<道>(文件名);
convertDocToHtm2< Excel的>(文件名);
 

解决方案

没有,这是不可能的。类型参数是的类型的,不会的命名空间的。

在特定的,编译器无法验证这种类型的,甚至存在 - 你可以称之为 ConvertDocToHtm2<系统> 例如:

使用动态类型在C#4,你的可以的做一些这样的:

 私人无效ConvertDocToHtm2< TApplication的>(字符串文件名)
    其中,TApplication的:新的()
{
     动态应用程序=新TApplication的();
     动态DOC = app.Documents.Open(文件名,HTML:trueValue);
     //这里其他的东西
}
 

然后:

  ConvertDocToHtm2< Word.Application>(文件名);
 

(我猜的参数名称 trueValue 的方式 - 你想验证)

I've got a function which takes a word document and saves it in html format. I'd like to use the same function to work with any document type. I've tried using generics (I'm assumming the different doc APIs are the same) which fails due to the reason Jon Skeet pointed out. Is there another way?

using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;

//Works ok
private void convertDocToHtm( string filename )
{
... snip

     var app = new Word.Application();
     var doc = new Word.Document();
     doc = app.Documents.Open(ref fileName, ref missing, ref trueValue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

... snip    
}

//fails dismally (to compile) because 'T' is a 'type parameter', which is not valid in the given context - i.e Word is a namespace not a class
private void convertDocToHtm2<T>( string filename )
{
... snip

     var app = new T.Application();
     var doc = new T.Document();
     doc = app.Documents.Open(ref fileName, ref missing, ref trueValue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

... snip
}

//calling examples
convertDocToHtm( filename );
convertDocToHtm2<Word>( filename );
convertDocToHtm2<Excel>( filename );

解决方案

No, this isn't possible. Type parameters are for types, not namespaces.

In particular, the compiler couldn't verify that such a type even existed - you could call ConvertDocToHtm2<System> for example.

With dynamic typing in C# 4, you could do something this:

private void ConvertDocToHtm2<TApplication>(string filename)
    where TApplication : new()
{
     dynamic app = new TApplication();
     dynamic doc = app.Documents.Open(filename, html: trueValue);
     // Other stuff here
}

Then:

ConvertDocToHtm2<Word.Application>(filename);

(I've guessed at the parameter name for trueValue by the way - you'd want to verify that.)

这篇关于传递一个命名空间到一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆