如何访问私有方法 [英] How to access a private method

查看:53
本文介绍了如何访问私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个类中有一个方法.我想从另一个班级获得那个私有方法.为此,我需要在该私有方法的类具有getter和setter的类中创建一个公共方法.这样我就可以访问该私有方法.该公共方法的外观如何?可以给我语法吗以下是我的私人方法.


there is a method in a class. I wanna acccess that private method from another class . To do that I need to create a public method inthat class whcih has getter and a setter for that private method. So that I can access that private method.how would that public method look like? can I have the syntax please. Following is my private method.


private void TryLoadTransitionInfo()

public --return type------? AccessPrivateMethod
{

         get { return TryLoadTransitionInfo()};
}


这几乎是正确的吗?我可以给一个合适的人吗?/


is this almost right? can i have the right one please/

推荐答案

如果您可以访问该类的源代码,则可以将修饰符从private更改为public .

If you have access to the source code for that class you can change the modifier from from private to public.

public void TryLoadTransitionInfo()
{
    // the code as is
}



如果您无权访问源,则必须使用反射,如下所示:



If you do not have access to the source you will have to use reflection as follows :

// obj is the object which has the TryLoadTransitionInfo method in it
MethodInfo dynMethod = obj.GetType().GetMethod("TryLoadTransitionInfo", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod.Invoke(obj, new object[] { });


您可以按照以下方式使用吸气剂:

You can use a getter as you have done:

public --return type------? AccessPrivateMethod
{

            get { return TryLoadTransitionInfo();}
}



看看半冒号也移到了哪里!!!



Look at where the semi colon has moved too!!!


我想说的是,我认为您在这里追求的策略不是好的OO设计.如果要发布对类中方法的访问权限,为什么不如其他人所评论的那样首先将其公开?

如果您无法访问定义该方法的源代码,那么我认为Mehdi通​​过反射解决方案是唯一可以继续进行的方法.

但是,在这种情况下,我相信您可以根据您的示例访问源.

如果要返回方法,则在.NET 3.5或更高版本中,可以使用Action< t>对于没有返回值的方法或Func t对于确实具有返回值的方法.请参阅MSDN文档,以了解Action和Func的指定参数类型的数量可以变化(重载).

在您的情况下,可能是这样的:
I would like to say that I think the strategy you are pursuing here is not good OO design. If you want to publish access to a method in a Class, why not, as others commented, make it public in the first place ?

If it''s the case that you don''t have access to the source code in which the method is defined, then I think Mehdi''s solution via reflection is the only way you can proceed.

But, in this case, I do believe you have access to the source based on your example.

If you want to return a method, in .NET 3.5 or later, you can use Action<t> for a method that has no return value, or Func<t> for a method that does have return. See the MSDN docs to understand that the number of specified parameter types to Action and Func can vary (the overloads).

Here''s how that might work in your case:
public class RevealprivateMethod
{
    private int addTwo(int i, int j)
    {
        return (i + j);
    }

    // create a public property of a method
    // whose signature is two int parameter inputs
    // and an int return value
    public Func<int, int, int> accessAddTwo
    {
        get { return addTwo; }
    }
}

要从类外部使用''RevealPrivateMethod中的方法,例如:

To use the method in ''RevealPrivateMethod from outside the class, for example:

RevealprivateMethod rpmInstance = new RevealprivateMethod();
int result = rpmInstance.accessAddTwo(5, 8);


这篇关于如何访问私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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