- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
问题:
大家好,我遇到了一个奇怪的问题,它开始让我非常沮丧。我到处寻找答案,并尝试了许多不同的方法,但由于某种原因,我无法使这段代码正常工作。
我正在使用反射根据 T 的类型将匿名对象数组转换为特定类型。现在在提到它之前,是的,反射是做我正在做的事情的唯一方法(将脚本语言转换为 Type Strong C# 对象)。
我的问题在第 35 - 43 行。如果值类型是 List{T} 的类型,那么我首先检查它是否为空...如果是,我使用 Activator.CreateInstance() 创建一个新列表(第 58 行),否则使用现有列表。我在这里面临的问题是调用此方法时值始终为 NULL,即使我在第 43 行设置值。这意味着列表中的项目永远不会超过 1,即使我添加更多超过 1 项...
在第 23 行和第 51 行,您可以看到我为其他类型设置了值,并且效果很好,那么我在第 34-43 行做错了什么导致这段代码无法正常工作?
代码:
您可以在 pastebin 上查看带有行号的代码或以下:
public class ObjectProperty<T> : ObjectPropertyBase
{
/// <summary>
/// Gets or Sets the value for this property
/// </summary>
public T Value;
/// <summary>
/// Takes an array of string values, and converts it to the proper value type for
/// this instance's Generic Type
/// </summary>
/// <param name="ValueParams">The string value's to convert, and set the
/// Value of this instance to.
/// </param>
public override void SetValueFromParams(String[] ValueParams)
{
Type PropertyType = typeof(T);
// Check for array's, as they are handled differently
if (PropertyType.IsArray)
{
// Set the value to the instanced object
Value = (T)ConvertArray(ValueParams, PropertyType);
}
else if (PropertyType.IsGenericType)
{
// Grab our types interfaces and generic types
Type[] interfaces = PropertyType.GetInterfaces();
Type[] types = PropertyType.GetGenericArguments();
// Check for List<T>
if (interfaces.Any(i => i == typeof(IList)))
{
// Grab our current list... if the Value isnt created yet, make it
dynamic obj = (dynamic)Value ?? CreateList(types);
// Add our value to the list
if (types[0].IsArray)
obj.Add(ConvertArray(ValueParams, types[0]));
else
obj.Add(ConvertValue<dynamic>(ValueParams[0], types[0]));
Value = (T)obj;
}
else
throw new Exception($"Invalid Generic Type found \"{PropertyType}\"");
}
else
{
// Since we are not an array, extract our only value
Value = ConvertValue<T>(ValueParams[0], PropertyType);
}
}
protected static IList CreateList(Type[] genericTypes)
{
Type obj = typeof(List<>).MakeGenericType(genericTypes);
return (IList)Activator.CreateInstance(obj);
}
/// <summary>
/// Converts the object value into the Typed variant of this <see cref="Value"/>
/// </summary>
protected K ConvertValue<K>(object Value, Type PropertyType)
{
// No need to change type if types match
if (Value.GetType() == PropertyType)
return (K)Value;
// Enums need special care
if (PropertyType.IsEnum)
return (K)Enum.Parse(PropertyType, Value.ToString(), true);
return (K)Convert.ChangeType(Value, PropertyType);
}
/// <summary>
/// Converts the object array values into the Typed variant of this <see cref="Value"/>
/// </summary>
protected object ConvertArray(Object[] values, Type propertyType)
{
// Set the value to the instanced object
switch (propertyType.Name.ToLowerInvariant())
{
case "int[]":
case "int32[]":
return Array.ConvertAll(values, Convert.ToInt32);
case "string[]":
return Array.ConvertAll(values, Convert.ToString);
case "double[]":
return Array.ConvertAll(values, Convert.ToDouble);
default:
throw new Exception("Invalid property type: " + propertyType.Name);
}
}
}
感谢您查看我的问题,希望有人能帮我解决这个问题。
编辑:调用方法
public class Example
{
[PropertyName("isExample")]
public ObjectProperty<bool> IsExample;
// .....
public void Parse(string[] Params)
{
// Fetch our property that we are setting the value to
FieldInfo prop = this.GetType().GetField("isExample");
// Create our instance, and parse
ObjectPropertyBase obj = ObjectPropertyBase.Create(prop);
obj.SetValueFromParams(Params);
prop.SetValue(this, obj);
}
}
// Base Class
public abstract class ObjectPropertyBase
{
/// <summary>
/// Takes an array of string values, and converts it to the proper value type for
/// this instance's Generic Type
/// </summary>
/// <param name="ValueParams">The string value's to convert, and set the
/// Value of this instance to.
/// </param>
public abstract void SetValueFromParams(String[] value);
/// <summary>
/// Using reflection, this method creates a new instance of the <paramref name="field"/>
/// type, and returns it.
/// </summary>
/// <remarks>
/// This method does NOT set the field value, since hey... we dont have an instance to work on
/// </remarks>
/// <param name="field">The field we are creating an instance for. </param>
/// <exception cref="System.Exception">
/// Thrown if the Field provided does not contain the <see cref="PropertyName"/> attribute
/// </exception>
public static ObjectPropertyBase Create(FieldInfo field)
{
// If the Custom attribute exists, we add it to the Mapping
Attribute attribute = Attribute.GetCustomAttribute(field, typeof(PropertyName));
if (attribute == null)
throw new Exception($"Internal property \"{field.Name}\" does not contain a PropertyName attribute!");
// Get our constructor
PropertyName fieldAttr = attribute as PropertyName;
return (ObjectPropertyBase)Activator.CreateInstance(
field.FieldType,
new object[] { }
);
}
}
示例:
最佳答案
我发现我做错了什么。我的反射代码没有任何问题,而是调用“SetValueFromParams”方法的代码有问题。在用新实例(以及新列表)覆盖它之前,我并没有首先检查 C# 对象的 ObjectProperty 字段是否为 null。
修复:
public void Parse(string[] Params)
{
// Fetch our property that we are setting the value to
FieldInfo prop = this.GetType().GetField("IsExample");
ObjectPropertyBase obj;
// Get the value that is set
var value = prop.Value.GetValue(this);
// If the value is null, then we create a new object property instance
if (value == null)
obj = ObjectPropertyBase.Create(prop.Value);
else
obj = (ObjectPropertyBase)value;
// Set Values
obj.SetValueFromParams(Params);
prop.SetValue(this, obj);
}
感谢大家花时间看我的问题!
关于c# - 使用反射获取通用列表始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680117/
当我使用路径文件上的快捷方式在文件之间移动时,似乎我不仅仅是在文件之间移动。 我使用>转到一个文件,在该文件中我更改光标的位置并执行某些操作,然后按 gf noremap 关于vim 通过快捷方式直
我正在尝试使用 Pong P. Chu 的书来学习 Verilog。我有一个关于如何评估和实现始终 block 的问题。作者代码中的风格让我感到困惑。 在此示例中,他编写了一个具有两个输出寄存器“y1
我正在尝试制作一个聊天应用程序,因此我需要它始终接收服务器信息。因此,当请求完成时,在: http.onreadystatechange=function(){ 我再次调用该函数,因此: reques
当您在 always block 敏感度列表中使用通配符 @* 时,我对什么被视为输入有点困惑。例如,在下面的示例中,哪些信号被解释为导致 always block 被重新评估的输入? 据我了解,cl
我有一个充当调试器的程序。我为线程设置了一个 hw bp,将 dr0 设置为我希望 bp 所在的地址,将 dr7 设置为 1,因为我希望 bp 在每次执行该地址时生成一个事件。 它有效,但现在的问题是
如何每次都以管理员身份在 Windows 上运行 git bash。 操作系统 - Windows 10 家庭版 64 位 最佳答案 我在 Google 上找到了这个结果: 将 Git Bash 设置
使用 accept() 时或 getpeername() , sockaddr_storage总是有 ss_family=AF_INET6 : struct sockaddr_storage addr
我在 Cordova 方面还有另一个问题。我想在 Cordova 7.1.0 中使用插件“cordova.custom.plugins.exitapp”和“cordova-plugins-printe
我试图让模块通过 ISE 12.4 中的语法检查,但它给了我一个我不明白的错误。首先是代码片段: parameter ROWBITS = 4; reg [ROWBITS-1:0] temp; genv
我正在使用Cordova开发适用于iOS的应用程序,其中包括地理位置功能(我使用官方插件https://github.com/apache/cordova-plugin-geolocation)。我在
我想知道是否有可能只在敏感列表中的多个信号一起变化时才执行 always block 。 例如,假设我有一个信号“in”和另一个“posedge clk”。我希望在两个信号都发生变化时执行 alway
我需要实现一种算法来访问数据库来检查最后一个元素,以便计算新的元素。当然,第一次这是不可能的,因为数据库是空的,我得到 IndexOutOfBoundsException) index 0 reque
我正在利用我在网上找到的画廊系统,根据鼠标图像的接近程度,它会按比例增长。 链接:Gallery 好吧,我调整了代码以响应(如您所见正在 build 中)并且没有明显的问题。我的问题在更改分辨率时开始
我正在创建一个 kiosk 应用程序,我想确保它无论如何始终位于其他 Windows 应用程序和 Windows 任务栏之上。 我已经阻止了 Windows 键盘命令(alt-tab 等),但仍有可能
我即将开始一个新的 React 项目,并尝试利用我以前的知识来创建一些关于我如何构建应用程序的规则。 有些事情我认为是真的: Redux 保存整个应用程序的“主要”数据 如果需要跨应用程序共享,Red
当你打开 VS Code 时,终端默认是在底部打开的。您可以单击该图标将其向右移动。我想知道是否有办法将右侧打开设置为默认值。 谢谢。 最佳答案 是的 - 在 v1.20 中引入了设置 workb
我有一个Events表,其中包含各种类型的事件。我只关心其中一种类型。因此,我编写的每个查询都以开头 Events.objects.filter(event_type="the_type").\
我在单例中创建了一个Timer,并且我一直在努力解决为什么Timer没有触发。我查看了这里的帖子,但没有找到我认为可以直接回答我的问题的帖子。 class ConnectionStateMonitor
我在 TableViewController 中显示了一组项目。它们在 TVC 中正确显示。下面的代码会继续,但它只会继续到我的 MKMapItem 数组的 indexPath 0,而不是被单击的单元
我的 VC 是这样的: var coins = 50 // coins override func viewDidLoad() { super.viewDidLoad() if(SKP
我是一名优秀的程序员,十分优秀!