Assemblie.load.getType 不适用于具有的类型?



我有一个类似110000多行的巨大代码文件,我正在为一些特定类型读取它,以获取有关它们的信息。我从.json文件中获取这些类型。

读出部分通过反射完成(组件在另一个解决方案中(:

首先,我将文件加载到内存中:

public static List<Assembly> LoadAssemblies(List<string> loadedPaths)
{
foreach (string loadPath in loadedPaths)
{
List<AssemblyName> referencedAssemblyNames = Assembly.LoadFrom(loadPath)
.GetReferencedAssemblies()
.Where(a => a.FullName.StartsWith(Constants.NameSpace))
.ToList();
}
return AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith(Constants.NameSpace)).ToList();
}

之后,我读出了我想要了解的特定类型及其自定义属性:

public static Dictionary<string, string> GetCustomAttributes(List<Type> streamDesign, List<Type> enums)
{
List<PropertyInfo> propertyInfos = new List<PropertyInfo>();
streamDesign.ForEach(sd => propertyInfos.AddRange(sd.GetProperties().ToList()));
Dictionary<string, string> dbUsageMapping = new Dictionary<string, string>();
foreach (Type t in enums)
{
var property = propertyInfos
.Where(p => p.PropertyType.FullName != null ? p.PropertyType.FullName.Equals(t.ToString(),
StringComparison.CurrentCultureIgnoreCase) : false)
.ToList();
foreach (PropertyInfo p in property)
{
var tmp = p.GetCustomAttributes()
.Where(a => a is ColumnAttribute)
.FirstOrDefault() as ColumnAttribute;
if (tmp != null)
{
dbUsageMapping.Add(t.ToString(), tmp.Name);
break;
}
}
}
return dbUsageMapping;
}

我用这个得到的类型:

public static List<Type> GetStreamDesignTypes(string loadedPath)
{
return AppDomain.CurrentDomain.GetAssemblies().Where(a => a.Location.Equals(loadedPath)).FirstOrDefault().GetTypes().ToList();
}

它适用于没有"&quot--&gt;

public global::JobTemplateType TemplateType {}

但是像这样的类型-->

public global::JobTemplateType? TemplateType {}

不在里面。

我必须做一些特定的事情才能得到这些类型的东西吗?如果我搜索带有"&";,则返回null;

有什么想法吗?

编辑:根据要求,原始类型和整个代码-->

public enum JobTemplateType
{
/// <summary>
/// Constant to represent None.
/// </summary>
None = 0,
/// <summary>
/// Constant to represent a normal job.
/// </summary>
Normal = 1,
...
}

原始类型是Enum,我想将Enum与CustomAttributes";name";(如果存在(以获取数据库中的事件。(用于文件编制(

public static void start(string path)
{
// First, I get the Json Data which contains the enums I want Information about and the assembly paths
/*
* 
{
"Enums":[
"Common.StreamVersionType",
"Common.JobTemplateType",
"Common.JobTemplateType?"
],
"ReturnCodeNames":[
"Common.JobReturnCodes"
],
"assembliesToLoad":[
"path.Common.dll",
"path.Components.dll"
],
"ReturnCodes": true
}
*/
DocumentationConfig jsonConfig = FileReader.GetJsonConfig();

// Now I load all the assemblies in the config
var loadedAssemblies = AssemblieHelper.LoadAssemblies(jsonConfig.AssembliesToLoad);
// Now I get the types of the huge class I want information about
var streamDesign = AssemblieHelper.GetStreamDesignTypes(jsonConfig.AssembliesToLoad.Where(a => a.Contains("Business.Components.dll")).FirstOrDefault());
// After that, I search for the EnumTypes I have in my .json --> this returns the 3 EnumsTypes I want
var enumTypes = AssemblieHelper.GetTypes(jsonConfig.Enums, loadedAssemblies);
// with those enumTypes, I want to get all ocurrences in the huge class File where the customeAttributes (i.e. DbName) are stored
var customAttributes = AssemblieHelper.GetCustomAttributes(streamDesign, enumTypes);


}

这里是getTypes函数,它返回EnumTypes:

public static List<Type> GetTypes(List<string> typeName, List<Assembly> loadedAssemblies)
{
List<Type> lEnums = new List<Type>();
foreach (Assembly assemblie in loadedAssemblies)
{
lEnums.AddRange(assemblie.GetTypes().Where(t => typeName.Contains(t.FullName)).ToList());
}
return lEnums;
}

edit2:示例-->

我得到的这个:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.StreamRunJob")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class StreamRunJob : INotifyPropertyChanging, INotifyPropertyChanged
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="TemplateTypeCd", Storage="_TemplateType", DbType="TinyInt NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=59)]
[GeneratedCodeAttribute("SqlMetal", "")]
public global::Common.JobTemplateType TemplateType
{
get
{
return this._TemplateType;
}
set
{
if ((this._TemplateType != value))
{
this.OnTemplateTypeChanging(value);
this.SendPropertyChanging();
this._TemplateType = value;
this.SendPropertyChanged("TemplateType");
this.OnTemplateTypeChanged();
}
}
}
}

这不是:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Job")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class Job : INotifyPropertyChanging, INotifyPropertyChanged
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="TemplateTypeCd", Storage="_TemplateType", DbType="TinyInt", CanBeNull=true, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=20)]
[GeneratedCodeAttribute("SqlMetal", "")]
public global::Common.JobTemplateType? TemplateType
{
get
{
return this._TemplateType;
}
set
{
if ((this._TemplateType != value))
{
this.OnTemplateTypeChanging(value);
this.SendPropertyChanging();
this._TemplateType = value;
this.SendPropertyChanged("TemplateType");
this.OnTemplateTypeChanged();
}
}
}
}

实际上,阅读您的代码,我认为问题就在这里:

var property = propertyInfos
.Where(p => p.PropertyType.FullName != null ? p.PropertyType.FullName.Equals(t.ToString(),
StringComparison.CurrentCultureIgnoreCase) : false)
.ToList();

更新原来我忘了枚举是值类型,doh。。。。

为什么不这样改呢:

var property = propertyInfos
.Where(p => p.PropertyType == t || 
(t.IsValueType && p.PropertyType == typeof(System.Nullable<>).MakeGenericType(t))
.ToList();

相关内容

最新更新