使用继承扩展现有的Office方法 [英] Extend existing Office Method using inheritance

查看:68
本文介绍了使用继承扩展现有的Office方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用inhereitance创建一个自定义对象,该对象扩展了Office对象中现有的proptery方法?因此,让我们假设当前形式的此方法需要10个常量并返回一个对象。我希望能够扩展该方法,以便需要更多的常量。
例如,对象("索引").Object.Property.Method1(常量)

或者

工作表("Sheet1")。Cells.SpecialCells(xlCellTypeConstants)

我希望能够为Method1定义附加常量(或者在这个例子中是SpecialCells方法)。然后,我将使用一个子来创建方法的结果,当使用新的常量时

感谢

由于发生了红色,因此

Is it possible using inhereitance to create a custom object which extends an existing method of a proptery in an Office object? So lets asssume that this method in its current form takes 10 constants and returns an object.  I want to be able to extend the method so that it takes a few more contstants.

For example
Object("Index").Object.Property.Method1(constants)

or

Worksheet("Sheet1").Cells.SpecialCells(xlCellTypeConstants)

I want to be able to define additional constants for Method1 (or in this example the SpecialCells method). I will then have to run a sub to create the results of the method when using the new constants

Thanks

EM

推荐答案

您可以使用VSTO Power Tools实现的扩展方法。要使用扩展方法,必须使用静态方法创建静态类,其中第一个参数是您希望扩展的对象的对象类型,并使用"this"和"this"。表示您希望该方法为扩展方法的关键字。

例如,以下代码向Range和Worksheet类添加一个方法以检索字符串值:


You could use extension methods as implemented by the VSTO Power Tools. To use extension methods you must create a static class with static methods where the first parameter is the object type of the object you wish to extend and the use the "this" keyword to indicate that you wish the method to be an extension method.

For example, the following code adds a method to the Range and Worksheet classes to retrieve string values:

static class ExcelExtensions

{

    public static string GetString(this Excel.Range range)

    {

        return (range.Value2 == null || range.Value2.ToString().Trim().Length == 0) ? null : range.Value2.ToString();

    }



    public static string GetString(this Excel.Range range, int row, int column)

    {

        Excel.Range r = range.Cells[row, column] as Excel.Range;

        return r.GetString();

    }



    public static string GetString(this Excel.Worksheet sheet, int row, int column)

    {

        Excel.Range r = sheet.Cells[row, column] as Excel.Range;

        return r.GetString();

    }

}





这篇关于使用继承扩展现有的Office方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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