我的FxCop analasys中的某些程序集存在问题。 我们项目中的大多数组件都可以正常工作,但Api除外。 FxCop在135条消息之后停止分析它们,并带有例外。
这是我得到的例外情况。
读取模块时遇到以下错误 'TenForce.Execution.Api2.Implementation':无法解析成员 参考:[TenForce.Execution.Api2.Implementation,Version = 1.0.0.0, 文化=中性, 公钥=空] TenForce.Execution.Api2.Implementation.Helpers.IdGenerator
1<type parameter.T>+Wrapper
1 ::对象。
我已经确保所有需要的程序集在一起并且是项目的一部分,但我不知道如何解决这个问题。 有没有人遇到过这个?
修改 添加导致问题的类的源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Collections.ObjectModel;
namespace TenForce.Execution.Api2.Implementation.Helpers
{
/// <summary>
/// <para>This class is responsible for generating the various Ids for the Ts.</para>
/// </summary>
public static class IdGenerator<T> where T : class, IEquatable<T>, new()
{
/// <summary>
/// Wraps underlying object. Requires object to have int Id property.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Wrapper<T> : IEquatable<Wrapper<T>>
{
private PropertyInfo piId;
/// <summary>
/// Wrapper constructor
/// </summary>
/// <param name="instance"></param>
public Wrapper(T instance)
{
Object = instance;
Created = DateTime.Now;
foreach (var pi in instance.GetType().GetProperties())
{
if (pi.Name.Equals("id", StringComparison.OrdinalIgnoreCase))
{
piId = pi;
}
}
if (piId == null)
{
var fullName = instance.GetType().FullName;
throw new TypeInitializationException(fullName,
new Exception(string.Format("{0} is not compatible with IdGenerator. It requires int Id property to be present", fullName)));
}
}
/// <summary>
/// Gets or sets wrapped instance Id
/// </summary>
public int Id
{
get
{
return (int)piId.GetValue(Object, null);
}
set
{
piId.SetValue(Object, value, null);
}
}
/// <summary>
/// Creation date
/// </summary>
public DateTime Created;
/// <summary>
/// Wrapped instance
/// </summary>
public T Object;
/// <summary>
/// Implements IEquatable interface
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(Wrapper<T> other)
{
return Object.Equals(other.Object);
}
}
#region Private Fields
private static Wrapper<T>[] _mWrappers = new Wrapper<T>[10];
private static readonly object MPadLock = new object();
#endregion
#region Public Members
/// <summary>
/// <para>Generates a new Id for the T and assigns it to the T.</para>
/// </summary>
/// <param name="obj">The T that needs a new Id generated.</param>
/// <remarks>The T will be stored inside the generator to keep track of the Id and availability.</remarks>
public static void GenerateId(T obj)
{
lock (MPadLock)
{
// Search the array for an empty spot or an expired T.
int index = Array.FindIndex(_mWrappers, s => s == null || DateTime.Now.Subtract(s.Created).Minutes > 10);
var wrapper = new Wrapper<T>(obj);
// If we found such a spot, store the new T in that location,
// If we did not find such a spot, expand the array and the T at the end.
if (index > -1) _mWrappers[index] = wrapper;
else
{
Array.Resize(ref _mWrappers, _mWrappers.Length + 1);
_mWrappers[_mWrappers.Length - 1] = wrapper;
}
// Always update the Id of the T entity with the negative index of the entity's location.
wrapper.Id = CalculateId(Array.IndexOf(_mWrappers, wrapper));
}
}
/// <summary>
/// <para>Releases the Id generated for the T and releases the location that was held by the T.</para>
/// </summary>
/// <param name="obj">The T that needs to be released.</param>
/// <remarks>The T will be removed from the generator and it's Id will be reset to zero !!!</remarks>
public static void ReleaseId(T obj)
{
lock (MPadLock)
{
var wrapper = new Wrapper<T>(obj);
if (wrapper.Id >= 0) return;
int index = Array.IndexOf(_mWrappers, wrapper);
Array.Clear(_mWrappers, index, 1);
wrapper.Id = 0;
}
}
/// <summary>
/// <para>Fetches the specified T from the IdGenerator.</para>
/// </summary>
/// <param name="id">The unique identifier of the T.</param>
/// <returns>The T with the matching Id or the default instance if not found.</returns>
public static T Fetch(int id)
{
lock (MPadLock)
{
var found = Array.Find(_mWrappers, s => s != null && s.Id == id);
return found == null ? new T() : found.Object;
}
}
/// <summary>
/// <para>Updates the matching T entity inside the Generator with the new one.</para>
/// </summary>
/// <param name="obj">The T that needs to be updated.</param>
public static void Update(T obj)
{
lock (MPadLock)
{
int index = Array.IndexOf(_mWrappers, obj);
if (index == -1) return;
var wrapped = new Wrapper<T>(obj);
_mWrappers[index] = wrapped;
wrapped.Id = CalculateId(index);
wrapped.Created = DateTime.Now;
}
}
/// <summary>
/// <para>Retrieves all the Ts currently available in the application.</para>
/// </summary>
/// <returns>An enumerable collection containing all the T entities.</returns>
public static IEnumerable<T> ListAll()
{
lock (MPadLock)
{
return new ReadOnlyCollection<T>(_mWrappers.Select(s => s == null ? null : s.Object).Where(o => o != null).ToList());
}
}
#endregion
#region Private Members
/// <summary>
/// <para>Calculates the negative id for an entity based upon the zero-based index.</para>
/// </summary>
/// <param name="index">The zero-based index of the entity who's Id needs to be calculated.</param>
/// <returns>The new Id for the entity.</returns>
private static int CalculateId(int index)
{
return (index * -1) - 1;
}
#endregion
}
}
答案 0 :(得分:3)
有几个已知问题会导致此类问题:https://connect.microsoft.com/VisualStudio/feedback/details/469754/code-analysis-fails-on-specific-conditions-involving-a-generic-class-containing-an-interface和https://connect.microsoft.com/VisualStudio/feedback/details/520967/code-analyze-brokes-while-loading-interface-from-a-separate-module-file-in-an-indirectly-referenced-assembly。如果这些似乎都不适用,请提供以下信息:
在问题中添加源代码后......
问题出现是因为IdGenerator<T>
和嵌套Wrapped<T>
类都使用相同的“T”类型参数名称。 C#编译器为此生成了警告(CS0693),您显然忽略了该警告。如果更改嵌套类的类型参数的名称,FxCop应该能够正确地分析类(并让你知道它包含的相当多的问题;)。
顺便说一下,如果你对代码质量很感兴趣,我强烈建议将编译器警告视为错误,如果你正在运行FxCop,你可能就是这样。为什么在最大限度地利用已经使用的工具之前添加额外的筛选工具?
答案 1 :(得分:1)
只需确保程序集是GAC,或者与您正在分析的程序集位于同一文件夹中。
旁注: 您可能想要省略程序集/命名空间/方法名称的实际名称。只是为了安全并避免麻烦。如果你知道我的意思:) *