我正在忙于创建一个汇编,它将收集CodeModel信息,而这些信息依次用于生成带有T4模板的代码。
由于缺乏良好的信息,我正在努力使用CodeModel。我找到了一些描述CodeModel的书籍,但只有真正的基础知识。没有找到深入的文档。
过去一周我创建了上面提到的程序集,以下构造工作正常超过4天。
/// <summary>
/// The CodeType of the property
/// </summary>
public CodeType CodeType
{
get
{
if (!m_CodeTypeInitialized)
{
CodeTypeRef codeTypeRef = CodeProperty.Type;
m_CodeType = codeTypeRef.CodeType;
m_CodeTypeInitialized = true;
}
return m_CodeType;
}
}
昨天突然这个构造不再返回CodeType了。我现在已将代码更改为此
/// <summary>
/// The CodeType of the property
/// </summary>
public CodeType CodeType
{
get
{
if (!m_CodeTypeInitialized)
{
if (CodeProperty.IsCodeType)
{
CodeTypeRef codeTypeRef = CodeProperty.Type;
m_CodeType = codeTypeRef.CodeType;
}
m_CodeTypeInitialized = true;
}
return m_CodeType;
}
}
这不再导致异常,但结果始终为“null”。我迷路了。什么可能导致CodeProperty突然松开它的CodeType?
我真的需要CodeType,因为很多代码都在关注它的信息。
答案 0 :(得分:0)
我能够像这样创造一个解决方案。这不好,但它运作良好:
private FileCodeModel m_FileCodeModel;
/// <summary>
/// The FileCodeModel the entity of this property is found in.
/// </summary>
public FileCodeModel FileCodeModel
{
get
{
if (m_FileCodeModel == null)
{
m_FileCodeModel = EntityMetadata.FileCodeModel;
}
return m_FileCodeModel;
}
}
private Project m_Project;
/// <summary>
/// The project this properties entity is contained in.
/// </summary>
public Project ContainingProject
{
get
{
if (m_Project == null)
{
m_Project = FileCodeModel.Parent.ContainingProject;
}
return m_Project;
}
}
private CodeModel m_CodeModel;
/// <summary>
/// The CodeModel for the properties entity.
/// </summary>
public CodeModel CodeModel
{
get
{
if (m_CodeModel == null)
{
m_CodeModel = ContainingProject.CodeModel;
}
return m_CodeModel;
}
}
/// <summary>
/// De CodeType van de property
/// </summary>
public CodeType CodeType
{
get
{
if (!m_CodeTypeInitialized)
{
if (CodeProperty.IsCodeType)
{
CodeTypeRef codeTypeRef = CodeProperty.Type;
m_CodeType = codeTypeRef.CodeType;
}
else
{
m_CodeType = CodeModel.CodeTypeFromFullName(CodeProperty.Type.AsFullName);
}
m_CodeTypeInitialized = true;
}
return m_CodeType;
}
}