将对象类型作为参数传递给函数 [英] Passing object type as parameter in function

查看:130
本文介绍了将对象类型作为参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的对象类,例如:



I have different object classes like:

public object1{
  public string prop1 {get; set;}
  public int prop2 {get; set;}
}

public object2 {
  public int prp1 {get; set;}
  public bool prp2 {get; set;}
}





我想制作一个接受这些对象作为参数的通用函数,





I want to make a common function which accepts these objects as parameter as,

SetValues(object1);
SetValues(object2);





和SetValues函数对象将根据其类型创建,值将被填入他们的属性类似于:





and in SetValues function object will be created according to their type and values will be filled in their properties something like;

SetValues(objectType T){ }





可以使用泛型或抽象方法或其他更好的方法在C#中实现。



can this be achieved using generics or abstract methods or some other better way in C#.

推荐答案

这些不是类,你是向我们展示。使用class关键字创建类。喜欢这个



These are not classes, that you're showing us. Classes are made using the class keyword. Like this

public class Object1 {
   public string prop1 {get; set;}
   public int prop2 {get; set;}
}
 
public class Object2 {
   public int prop1 {get; set;}
   public bool prop2 {get; set;}
}





您可以使用相同的名称不同的班级之间。没有必要对它们进行不同的命名,因为它们位于不同的类中。但是你没有把它们放在不同的课程中,你在不同的不知道的情况下将它们放在不同的东西中,无论是一类还是功能或任何东西。



一旦你这样做,你就会有课程。要创建这样的函数,我想你会这样做





You can have same names among different classes. There is no need to name them differently, since they're in different classes. But you weren't having them in different classes, you were having them in different don't-know-whether-that-was-a-class-or-function-or-whatever thing.

Once you do so you will have the classes. To create such a function I think you will do this

SetValues(T){
   // go your code here. It will accept the objects
}





您可以传递第一个对象或第二个对象。您有一个额外的参数数据类型,objectType,这会导致编译器错误。 C#允许泛型类型,因此可以让编译器处理将对象强制转换为更好的内容。



You can pass either first object or the second. You were having an extra parameter data type, of objectType, which would have caused compiler error. C# allows generic types, so it is ok to let the compiler handle what would be better to cast the object to.


Afzaal Ahmad Zeeshan的所有内容都是正确的。



我能想到的唯一方法是使用一个接口,在object1和object2中实现接口。



然后你就可以了通过使用对实现该接口的对象的接口引用来调用它



Everything that Afzaal Ahmad Zeeshan is correct.

The only way I can think of is by using an interface where the interface is implemented in object1 and object2.

You could then call it by using an interface reference to an object that implements that interface

interface IValues
{
  void SetValues();
}





然后你的课程就会实现这个





Then your classes would implement this

public class object1 : IValues
{
  public void SetValues(IValues refIvalues);
}

public class object2 : IValues
{
  public void SetValues(IValues refIvalues);
}





通过引用参数中的接口IValues,它只接受实现IValues的类或对象。通过这种方式,编译器会让您通过正确的类型



By referencing interface IValues in the paramaters it will only accept classes or objects that implement IValues. This way you are gaurenteed by the compiler that you will get passed the correct type


这篇关于将对象类型作为参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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