如何从基础抽象类中访问类属性

huangapple go评论123阅读模式
英文:

How To Access Class Properties From A Base Abstract Class

问题

I'm trying to design a base abstract class that can gather all the properties from classes that are inherited from it. I'm getting the name fine, but getting the value is not working. I get the runtime error "Object does not match target". Any help is much appreciated.

  1. public string Get()
  2. {
  3. string ret = "";
  4. var type = GetType();
  5. foreach (PropertyInfo p in this.GetType().GetProperties())
  6. {
  7. ret += p.Name + "`" + p.GetValue(type) + "~";
  8. //i've also tried
  9. //ret += p.Name + "`" + p.GetValue(this) + "~";
  10. }
  11. ret = ret.TrimEnd('~');
  12. return ret;
  13. }
英文:

I'm trying to design a base abstract class that can gather all the properties from classes that are inherited from it. I'm getting the name fine, but getting the value is not working. I get the runtime error "Object does not match target". Any help is much appreciated.

  1. public string Get()
  2. {
  3. string ret = "";
  4. var type = GetType();
  5. foreach (PropertyInfo p in this.GetType().GetProperties())
  6. {
  7. ret += p.Name + "`" + p.GetValue(type) + "~";
  8. //i've also tried
  9. //ret += p.Name + "`" + p.GetValue(this) + "~";
  10. }
  11. ret = ret.TrimEnd('~');
  12. return ret;
  13. }

答案1

得分: 3

> 参数计数不匹配。

看起来其中一个属性是一个indexer,你可以通过检查PropertyInfo上的GetIndexParameters来忽略这样的属性(或者根据需要进行相应处理):

  1. if (!p.GetIndexParameters().Any())
  2. {
  3. ret += p.Name + "`" + p.GetValue(this) + "~";
  4. }

@sharplab.io上演示运行时异常。

英文:

> Parameter count mismatch.

It seems that one of the properties is an indexer, you can ignore such properties by checking GetIndexParameters on the PropertyInfo (or process it accordingly if needed):

  1. if (!p.GetIndexParameters().Any())
  2. {
  3. ret += p.Name + "`" + p.GetValue(this) + "~";
  4. }

Demo with the runtime exception @sharplab.io.

答案2

得分: 2

需要传递要获取属性值的对象

p.GetValue(this)

英文:

You need to pass the object you want to get property values from

p.GetValue(this)

答案3

得分: 1

When working with arbitrary classes, we should take into account, that we can have

  • static properties: public static int MyId => 3;
  • Indexers public int this[int index] { get => index;}
  • Write only properties public int MyId {set => m_Id = value;}

So far so good, let's query the properties:

  1. using System.Linq;
  2. ...
  3. public string Get() => string.Join(""~"", GetType()
  4. .GetProperties()
  5. .Where(prop => prop.CanRead) // readable
  6. .Where(prop => !prop.GetGetMethod().IsStatic) // instance
  7. .Where(prop => prop.GetIndexParameters().Length == 0) // not an indexer
  8. .Select(prop => $""{prop.Name}" `{prop.GetValue(this)}""));

Another issue is string manipulations: if you have a lot of properties (long ret) adding strings can be slow. If you want to join strings, just put string.Join.

英文:

When working with arbitrary classes, we should take into account, that we can have

  • static properties: public static int MyId => 3;
  • Indexers public int this[int index] { get => index;}
  • Write only properties public int MyId {set => m_Id = value;}

So far so good, let's query the properties:

  1. using System.Linq;
  2. ...
  3. public string Get() => string.Join("~", GetType()
  4. .GetProperties()
  5. .Where(prop => prop.CanRead) // readable
  6. .Where(prop => !prop.GetGetMethod().IsStatic) // instance
  7. .Where(prop => prop.GetIndexParameters().Length == 0) // not an indexer
  8. .Select(prop => $"{prop.Name} `{prop.GetValue(this)}"));

Another issue is string manipulations: if you have a lot of properties (long ret) adding strings can be slow. If you want to join strings, just put string.Join.

huangapple
  • 本文由 发表于 2023年6月27日 20:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定