CodeProperty'的CodeType突然消失了

本文关键字:CodeType 突然 消失了 CodeProperty | 更新日期: 2023-09-27 17:54:59

我正忙着创建一个程序集,该程序集将收集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因为很多代码都依赖于它的信息

CodeProperty'的CodeType突然消失了

我可以像这样创建一个工作区。这不是很好,但它工作得很好:

        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;
        }
    }