【发布时间】:2020-05-27 17:08:19
【问题描述】:
当我尝试在我的 c# .NET Framework 4.7.2 类库中打开一个表单时出现此错误。 检查自动生成绑定重定向
调用栈是
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectGetAttributes(Type type)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetAttributes()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
at System.ComponentModel.TypeDescriptor.GetAttributes(Type componentType)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetConverter(Object instance)
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter()
at System.ComponentModel.TypeDescriptor.GetConverter(Type type)
at System.ComponentModel.PropertyDescriptor.get_Converter()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetCollectionOfBoundDataGridViewColumns()
at System.Windows.Forms.DataGridView.RefreshColumns()
at System.Windows.Forms.DataGridView.RefreshColumnsAndRows()
at System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)
我查看了this question 并尝试在 .exe 的 app.config 中添加以下内容,但没有帮助
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="4.2.0.0" newVersion="4.7.0.0" />
</dependentAssembly>
实际的形式是在一个dll中,所以我也尝试将它添加到dll的app.config中
在我的项目文件中,我有
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
我发现 4.2.0 甚至在 Nuget 上都不可用。
我想知道我是否可以关闭尝试调用
DataGridView.DataGridViewDataConnection.GetCollectionOfBoundDataGridViewColumns()
在设计时
点击忽略并继续似乎是安全的。
Team Explorer Changes 表示这不会更改代码。
[更新]
如果我尝试将 DataGridView 控件复制并粘贴到另一个表单,我会收到处理此命令时发生错误。无法加载文件或程序集
我能够从工具箱中添加一个新的 DataGridView 控件,但是当我尝试将绑定源附加到该控件时
Error setting value MyBindingSource to property DataSource
[更新]
我发现绑定源的项目数据源已被删除。 那就是当我单击绑定源的数据源属性上的向下箭头时,数据源不在可供选择的列表中。
原来我忘记在源代码管理中包含项目属性 DataSources。
但是,当我添加一个新的 DataGridView 并尝试设置选择绑定源时,我仍然收到错误无法加载文件或程序集
绑定源基于一个对象。错误发生在特定对象而不是另一个对象上。也许它是导致问题的对象的属性。
[更新]
当绑定的对象包含一个也是对象的属性时会出现问题
public class BOMObj
{
// public FramePart FramePart { get; set; } // error occurs if I uncomment this
public string PartTypeName => $"{FramePart?.ComponentType}";
public string thing3 => "thing 3";
public string thing { get; set; }
public string thing2 { get; }
我尝试在构造函数中实例化 FramePart,但没有帮助
public BOMObj() {
FramePart = new FramePart();
}
我认为这段代码不能在设计时运行是有道理的。但是为什么缺少文件消息?
[更新] 因为this question,我非常有信心我的重定向没问题。请注意,该问题仅在设计时发生。
[更新]
查看 System.ComponentModel.Annotations 的进程监视器
但看不到任何失败消息。
我还尝试安装导致警告的 4.1.0 版
NU1605 Detected package downgrade: System.ComponentModel.Annotations from 4.7.0 to 4.1.0. Reference the package directly from the project to select a different version.
【问题讨论】:
-
试试
-
谢谢,但没有帮助
-
Fusion Log Viewer (fuslogvw.exe) 输出可能有助于找出更多细节。
-
谢谢奥列格,我更新了问题的结尾。该问题仅在设计时发生。
标签: c# winforms datagridview windows-forms-designer