Python clr 模块,AddReferenceByPartialName() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用clr.AddReferenceByPartialName()

项目:iph    作者:PMoureu    | 项目源码 | 文件源码
def try_import_clr_path(path_to_ref):
    ''' try add clr reference
        arg path_to_ref : string
    '''
    try:

        mods = [mod for mod in path_to_ref.split('.') if mod][::-1]
        entry_module = mods.pop()
        clr.AddReferenceByPartialName(entry_module)

        if sys.modules.has_key(entry_module):
            refobject = sys.modules[entry_module]
            while mods:
                mod = mods.pop()
                refobject = getattr(refobject, mod)
        else:
            for ref in clr.References:
                if ref.FullName.startswith(path_to_ref):
                    refobject = ref
                    break
            else:
                refobject = clr.References

    except Exception as error:
        refobject = None
        logger.debug('try_import_clr_path Backup import failed {} - {}'.format(path_to_ref, error))

    finally:
        return refobject