[英]Passing object attribute to method parameter C#
Lets say I have a person class with name and id and a animal class with the same attributes, and i have a list with persons and animals. 可以说我有一个具有名称和ID的人员类别,以及一个具有相同属性的动物类别,并且我有一个包含人员和动物的清单。 Now i want to make a method for returning the last id from that list and increment it.
现在,我想制作一种从该列表返回最后一个ID并将其递增的方法。 I want to make it universal so i can use it later.
我想使其通用,以便以后使用。
public static int getNextId(List<Object>param)
{
int lastId = Int32.Parse(param[param.Count - 1].id);
if (lastId!=0)
{
return lastId++;
}
return 0;
}
but the 'id' is underlined because object does not have id. 但是带下划线的是“ id”,因为对象没有ID。
Edit: 编辑:
something like this in python 像这样的Python
def someMethod(self, someList, attr):
objAttr = getattr(someObject, attr)
for(item in someList):
return item.objAttr
Your approach is not how you handle stuff like this in statically typed languages such as C#. 您的方法不是如何处理诸如C#之类的静态类型的语言。 You can only access properties / fields that are declared on the specific type.
您只能访问在特定类型上声明的属性/字段。 And
object
does not have a public field or property called id
. 而且
object
没有名为id
的公共字段或属性。
There are some ways around this: One would be having a base class that has an id
property from which your other classes could inherit: 可以通过以下方法解决:一种方法是拥有一个具有
id
属性的基类,您的其他类可以从该基类继承:
public class IdHolder
{
public int Id { get; set; }
}
public class Person : IdHolder
{
// Person inherits the 'Id' property from IdHolder
// other properties unique to person...
}
IdHolder
could just as well be an interface
or an abstract class
- it depends on your specific use case (note that you would have to implement the Id
property in each implementing class if you'd chose to make IdHolder
an interface). IdHolder
也可以是interface
或abstract class
-这取决于您的特定用例(请注意,如果您选择将IdHolder
为接口,则必须在每个实现类中实现Id
属性)。
If you chose to have a base class (or interface, ...) you'd change your method to accept that as a parameter: 如果选择具有基类(或接口,...),则将更改方法以将其接受为参数:
public static int getNextId(List<IdHolder>param)
{
int lastId = param[param.Count - 1].Id;
if (lastId!=0)
{
return lastId++;
}
return 0;
}
Another - slightly dirty - option is to use reflection. 另一个(稍微脏一点)的选项是使用反射。 As I don't think that this is a sensible route for you to take, I won't go further into this here.
因为我不认为这是明智的选择,所以在这里我将不做进一步介绍。
I'd advise you to have a look at an intro book into C# as there are some other aspects of your code that don't really follow C# guidelines (eg using camelCase instead of PascalCase). 我建议您看一下C#入门书籍,因为您代码中的某些其他方面并没有真正遵循C#准则(例如,使用camelCase代替PascalCase)。
Create a Person class with a Id Property like this: 创建一个具有Id属性的Person类,如下所示:
public class Person
{
public int Id {get; set;}
}
So you can use: 因此,您可以使用:
public static int getNextId(List<Person>param)
{
int lastId = param.Last().Id;
if (lastId!=0)
{
return lastId++;
}
return 0;
}
Another aproach is using interfaces to make "universal" like you said: 另一个方法是使用接口使“通用”成为您所说的:
public interface IId
{
int Id {get;set;}
}
public class Person : IId
{
public int Id {get; set;}
}
public static int getNextId(List<IId>param)
{
int lastId = param.Last().Id;
if (lastId!=0)
{
return lastId++;
}
return 0;
}
public static int GetNextId(List<object> param)
{
int id = param.OfType<Person>().Last().Id;
return ++id;
}
OfType method filters the elements based on a specified type. OfType方法根据指定的类型过滤元素。 Only people will be filtered from the list of people and animals.
从人和动物列表中只会过滤掉人。
Last method returns a last element of a sequence. Last方法返回序列的最后一个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.