VB.NET 取当前项目根命名空间

VB.NET 取当前项目根命名空间

在VB.NET中,可以使用反射来获取当前程序集的入口点类型(通常是模块或者主类),然后从这个类型的Assembly信息中获取定义它的程序集的根命名空间。以下是一个获取当前项目根命名空间的方法示例:

请注意,这个方法假设您的项目具有一个AssemblyTitleAttribute,并且这个Title属性反映了项目的根命名空间。如果没有设置AssemblyTitle,或者设置的方式不符合预期,那么可能需要调整代码来适应具体的项目结构。

vbnet 复制代码
  Dim currentNamespace As String = GetCurrentRootNamespace()
        MsgBox("当前项目根命名空间:" & currentNamespace)

    Function GetCurrentRootNamespace() As String
        '取根命名空间

        ' 获取入口点类型
        Dim assembly As Assembly = Assembly.GetExecutingAssembly()
        Dim entryType As Type = Assembly.GetEntryAssembly().GetTypes()(0)

        ' 获取当前程序集
        assembly = Assembly.GetAssembly(entryType)

        ' 获取当前程序集的属性
        Dim attributes As AssemblyTitleAttribute() = assembly.GetCustomAttributes(GetType(AssemblyTitleAttribute), False)

        ' 如果定义了AssemblyTitleAttribute,则使用它的Title作为根命名空间
        If attributes.Length > 0 Then
            Dim title As String = attributes(0).Title
            Dim nsIndex As Integer = title.IndexOf(".")
            If nsIndex > 0 Then
                Return title.Substring(0, nsIndex)
            End If
        End If

        ' 如果没有定义AssemblyTitleAttribute,则从入口点类型的全名中提取根命名空间
        Dim fullName As String = entryType.FullName
        If fullName IsNot Nothing Then
            Dim nsIndex As Integer = fullName.IndexOf(".")
            If nsIndex > 0 Then
                Return fullName.Substring(0, nsIndex)
            End If
        End If

        Return String.Empty
    End Function
相关推荐
xwLink19964 小时前
vb.net&cad二开自学笔记2:认识vs编辑器
vb.net·cad二次开发
token-go15 小时前
学习.NET 8 MiniApis入门
网络·学习·.net
液态不合群18 小时前
一款EF Core下高性能、轻量级针对分表分库读写分离的解决方案
c#·.net
yangshuquan19 小时前
总结一下 .NET FrameWork 和 .NET Core 创建的项目的不同点
c#·.net·基础知识
mingupup19 小时前
WPF在.NET9中的重大更新:Windows 11 主题
windows·.net·wpf
那个那个鱼1 天前
C#面:请写出C#中的单例模式
开发语言·单例模式·c#·.net
dot.Net安全矩阵2 天前
.NET 漏洞分析 | 某ERP系统存在SQL注入
数据库·sql·安全·web安全·矩阵·.net
dot.Net安全矩阵2 天前
.NET 漏洞情报 | 某整合管理平台SQL注入
数据库·sql·安全·矩阵·.net
白菜不太菜2 天前
.net 8 集成 MinIO文件存储服务,实现bucket管理,以及文件对象的基本操作
c#·.net·minio
程序猿经理2 天前
Java和.net有什么区别
java·开发语言·.net