如何获得通用 属性 但忽略其类型?
How to get generic property but ignore its type?
我有一个本质上是键值对的结构。我有另一个 class 具有这些 MyStruct 的多个属性(键是字符串,值是 T)或其他类型。我想获取所有属性并在值上调用它们的 ToString 函数,例如
foreach (var prop in AllTheProperties)
{
if (prop.GetType() is typeof(MyStruct<ignoreMe>)
{
yield return prop.Key;
yield return prop.Value.ToString();
}
}
但我卡住的地方是打字部分。我不想得到所有类型的字符串,然后键入int等。如何忽略类型? (附带说明,这将移植到 VB,但我更喜欢先在 C# 中做一些事情)。
用这个方法。它使用反射和 LINQ 来调用任何对象数组的所有 public 属性的 ToString。
public static IEnumerable<string> GetAllPropsAsStrings(object[] objs)
{
return from obj in objs from prop in obj.GetType().GetProperties() select prop.GetValue(obj).ToString();
}
使用两个不同键值对的示例用法,但这也适用于您自己的自定义结构:
KeyValuePair<string, int> blah = new KeyValuePair<string, int>("hello", 42);
KeyValuePair<int, int> blah2 = new KeyValuePair<int, int>(22, 42);
var stringarray = GetAllPropsAsStrings(new object[] {blah, blah2});
foreach (string str in stringarray)
{
Console.WriteLine(str);
}
我会为您的 MyStruct<T>
对象分配一个接口,该接口具有 string Key
的 属性 和 object ObjectValue
的只读 属性。然后我会在 ObjectValue
属性 的实现中只 return Value
。这是一个例子:
public interface GenericMyStruct
{
string Key { get; set; }
object ObjectValue { get; }
}
public class MyStruct<T> : GenericMyStruct
{
public string Key { get; set; }
public T Value { get; set; }
public object ObjectValue { get { return (object)Value; } }
}
写出所有属性的代码将如下所示:
foreach (var prop in AllTheProperties)
{
if (prop is GenericMyStruct)
{
yield return prop.Key;
yield return prop.ObjectValue.ToString();
}
}
如果您所追求的只是确保使用的泛型类型是 MyStruct 而不管 T,则您只需比较泛型类型定义。
Type propType = prop.GetType();
if(propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(MyStruct<>))
{
//Do work
}
如果类型为 MyStruct 的对象无论泛型类型参数如何,这将始终 return 为真。
信用:- How to get base class's generic type parameter?
我有一个本质上是键值对的结构。我有另一个 class 具有这些 MyStruct 的多个属性(键是字符串,值是 T)或其他类型。我想获取所有属性并在值上调用它们的 ToString 函数,例如
foreach (var prop in AllTheProperties)
{
if (prop.GetType() is typeof(MyStruct<ignoreMe>)
{
yield return prop.Key;
yield return prop.Value.ToString();
}
}
但我卡住的地方是打字部分。我不想得到所有类型的字符串,然后键入int等。如何忽略类型? (附带说明,这将移植到 VB,但我更喜欢先在 C# 中做一些事情)。
用这个方法。它使用反射和 LINQ 来调用任何对象数组的所有 public 属性的 ToString。
public static IEnumerable<string> GetAllPropsAsStrings(object[] objs)
{
return from obj in objs from prop in obj.GetType().GetProperties() select prop.GetValue(obj).ToString();
}
使用两个不同键值对的示例用法,但这也适用于您自己的自定义结构:
KeyValuePair<string, int> blah = new KeyValuePair<string, int>("hello", 42);
KeyValuePair<int, int> blah2 = new KeyValuePair<int, int>(22, 42);
var stringarray = GetAllPropsAsStrings(new object[] {blah, blah2});
foreach (string str in stringarray)
{
Console.WriteLine(str);
}
我会为您的 MyStruct<T>
对象分配一个接口,该接口具有 string Key
的 属性 和 object ObjectValue
的只读 属性。然后我会在 ObjectValue
属性 的实现中只 return Value
。这是一个例子:
public interface GenericMyStruct
{
string Key { get; set; }
object ObjectValue { get; }
}
public class MyStruct<T> : GenericMyStruct
{
public string Key { get; set; }
public T Value { get; set; }
public object ObjectValue { get { return (object)Value; } }
}
写出所有属性的代码将如下所示:
foreach (var prop in AllTheProperties)
{
if (prop is GenericMyStruct)
{
yield return prop.Key;
yield return prop.ObjectValue.ToString();
}
}
如果您所追求的只是确保使用的泛型类型是 MyStruct 而不管 T,则您只需比较泛型类型定义。
Type propType = prop.GetType();
if(propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(MyStruct<>))
{
//Do work
}
如果类型为 MyStruct 的对象无论泛型类型参数如何,这将始终 return 为真。
信用:- How to get base class's generic type parameter?