English 中文(简体)
如何用C# 反对方法/通过目标点改变一个领域?
原标题:How to call a C# object method/ change a field through an object pointer?
  • 时间:2011-01-03 20:24:58
  •  标签:
  • c#

这里是一个容易的问题(这里是C# noob),我无法找到如何做到这一点(我有Schildt的C#4.0参考资料,但不能肯定我所期待的是什么)。

Saye 我有两个不同的班子制造了两个物体,但也有相同的田地和(或)方法。 例如:

public class Object1 {
  bool enable;
  public void Reset(){
    enable = false;
  }
}

public class Object2 {
  bool enable;
  public void Reset(){
    enable = false;
  }
}

物体与异构体相同,因此有理由制造两个不同的物体。 然而,我有许多目标有共同的外地和(或)回收方法。

因此,我在此想做的是(在假装编码中,或者在什么情况下):

void Manipulate(ref object pObj){
  (pObj).enable = true;
  (pObj).Reset();
}

void Main(){
  Manipulate(obj1);
  Manipulate(obj2);
}

我如何能够这样做? 我不敢确定如何制造能够指不同类别物体的物体。

感谢!


Thanks to all the answers. I went through interfaces and understood them as an alias and couldn t figure out what was the usefulness of them.

最佳回答

就是:

public interface ICanResetAndEnable
{
    // Note: interfaces cannot have fields, only properties and methods.
    bool Enable { get; set; }
    void Reset();
}

public class Object1 : ICanResetAndEnable {
  bool Enable { get; set; }
  public void Reset(){
    Enable = false;
  }
}

public class Object2 : ICanResetAndEnable {
  bool Enable { get; set; }
  public void Reset(){
    Enable = false;
  }
}

您然后界定了您的<代码>Manipulate方法,以接受实施这一接口的任何类型的参数:

void Manipulate(ICanResetAndEnable obj)
{
    // Do whatever.
}
问题回答

鉴于你正在使用。 NET 4.0 你可以简单地宣布你们的变数是动态的,并且使用晚才具有约束力的方法。

dynamic myvar = GetTheObjectFromSomewhere();
myVar.Enable = true;
myVar.Reset();

Just use a common interface that Object1 and Object2 both implement.

public interface InterfaceA {
  bool Enable { get; set; }
  void Reset();
}

public class Object1 : InterfaceA {
  public bool Enable { get; set; }
  public void Reset(){
    Enable = false;
  }
}

public class Object2 : InterfaceA {
  public bool Enable { get; set; }
  public void Reset(){
    Enable = false;
  }
}

void Manipulate(InterfaceA pObj){
  pObj.Enable = true;
  pObj.Reset();
}

void Main(){
  Manipulate(obj1);
  Manipulate(obj2);
}

注:我改动了你对公有自动财产的公有属性,因为接口不能有数据成员,也无法运作。 此外,使用财产也是更好的做法。

你们想要做这样的事情:

public interface IEnableable {
    bool Enable { get; set; }
}

public interface IResettable {
    void Reset();
}

public interface IEnableableAndResettable : IEnableable, IResettable { }

正在做的是,对你希望能够就你们的目标开展的行动进行抽象的判断。 然后,你可以采用<代码>Manipulate的食物物体符合这一抽象原则。

public class Object1 : IEnableableAndResettable {
    public bool Enable { get; set; }
    public void Reset() {
        this.Enable = false;
    }
    // details
}

public class Object2 : IEnableableAndResettable {
    public bool Enable { get; set; }
    public void Reset() {
        this.Enable = false;
    }
    // details
}

然后:

void Manipulate(IEnableableAndResettable enableableAndResettable){
    enableableAndResettable.Enable = true;
    enableableAndResettable.Reset();
}

您甚至可以考虑颁发<条码>。 a 共同基础:

public class Enableable : IEnableableAndResettable {
    public bool Enable { get; set; }
    public void Reset() { this.Enable = false; }
}

public class Object1 : Enableable { }
public class Object2 : Enableable { }

我将对所有共享方法检索的所有物体进行接口或基级;

接口:

public interface IResetable
{
    void Rest();
}

public class ClassA : IResetable
{
    bool Enable = true;
    public void Reset()
    {
      Enable = false;
    }
}

void Manipulate(IResetable obj){
    obj.Reset();
}

普通学校:

public class BaseResetClass 
{
    internal bool Enable = true;
    public void Reset()
    {
      Enable = false;
    }
}

public class ClassA : BaseResetClass 
{

}
public class ClassB : BaseResetClass 
{

}


var ca = new ClassA();
ca.Reset();

var ca2 = new ClassB();
ca2.Reset();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签