Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor usage of the Assembly.CodeBase property #422

Merged
merged 6 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Source/Excel/Tools/RealtimeDataServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,10 @@ private static void RegisterHandleCodebase(Type type, InstallScope scope)
bool isSystemComponent = location.IsMachineComponentTarget(scope);
Assembly thisAssembly = Assembly.GetAssembly(type);
string assemblyVersion = thisAssembly.GetName().Version.ToString();
#pragma warning disable SYSLIB0012 // Type or member is obsolete
// although `CodeBase` is obsolete, the COM registry value requires it
CodebaseAttribute.CreateValue(type.GUID, isSystemComponent, assemblyVersion, thisAssembly.CodeBase);
#pragma warning restore SYSLIB0012 // Type or member is obsolete

}
}
Expand Down
1 change: 1 addition & 0 deletions Source/NetOffice.Tests/NetOffice.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Tests\NoAssemblyTitle\NoAssemblyTitle.csproj" />
<ProjectReference Include="..\Access\AccessApi.csproj" />
<ProjectReference Include="..\Excel\ExcelApi.csproj" />
<ProjectReference Include="..\NetOffice\NetOffice.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using NetOffice.Diagnostics;
using NUnit.Framework;

namespace NetOffice.Tests.NetOffice
{
[TestFixture]
public class SelfDiagnosticsTests
{
[Test]
public void AssemblyTitle_AssemblyWithNoAttribute_ReturnTitleBasedOnFilename()
{
// Arrange
var addin = new NoAssemblyTitleAddin();
var diag = new SelfDiagnostics(addin);

// Act
var title = diag.AssemblyTitle;

// Assert
Assert.AreEqual("NoAssemblyTitle", title);
}
}
}
50 changes: 50 additions & 0 deletions Source/NetOffice.Tests/NetOffice/Loader/PathBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.IO;
using NetOffice.Loader;
using NUnit.Framework;

namespace NetOffice.Tests.NetOffice.Loader
{
[TestFixture]
public class PathBuilderTests
{
[Test]
public void BuildLocalPathFromDependentAssembly_SampleDependentAssembly_ResolvesPathToAssemblyFile()
{
// Arrange
var expectedAssemblyName = "NetOffice.Core.dll";
var assembly = new DependentAssembly(expectedAssemblyName, typeof(Core).Assembly);

// Act
var path = PathBuilder.BuildLocalPathFromDependentAssembly(assembly);

// Assert
StringAssert.EndsWith(expectedAssemblyName, path);
Assert.IsTrue(Path.IsPathRooted(path));
}

[Test]
[TestCaseSource(nameof(NetOfficeAssemblyNameTestCase))]
public void BuildLocalPathFromAssemblyFileName_NetOfficeAssemblyName_ResolvesPathToAssemblyFile(string assemblyName)
{
// Arrange
var factory = new Core();

// Act
var path = PathBuilder.BuildLocalPathFromAssemblyFileName(factory, assemblyName);

// Assert
StringAssert.EndsWith(assemblyName, path);
Assert.IsTrue(Path.IsPathRooted(path));
}

public static IEnumerable<string> NetOfficeAssemblyNameTestCase
{
get
{
return Core.Default.CoreDomain.AssemblyNames;
}
}
}
}
26 changes: 26 additions & 0 deletions Source/NetOffice.Tests/OfficeApi/Tools/AssemblyInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using NetOffice.Diagnostics;
using NetOffice.OfficeApi.Tools.Informations;
using NUnit.Framework;

namespace NetOffice.Tests.NetOffice
{
[TestFixture]
public class AssemblyInfoTests
{
[Test]
public void AssemblyTitle_AssemblyWithNoAttribute_ReturnTitleBasedOnFilename()
{
// Arrange
var addin = new NoAssemblyTitleAddin();
var ownerAssembly = addin.GetType().Assembly;
var diag = new AssemblyInfo(ownerAssembly);

// Act
var title = diag.AssemblyTitle;

// Assert
Assert.AreEqual("NoAssemblyTitle", title);
}
}
}
60 changes: 33 additions & 27 deletions Source/NetOffice.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@
"NetOfficeFw.Access": {
"type": "Project",
"dependencies": {
"NetOfficeFw.ADODBApi": "[1.9.3, )",
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.DAOApi": "[1.9.3, )",
"NetOfficeFw.MSComctlLibApi": "[1.9.3, )",
"NetOfficeFw.MSDATASRCApi": "[1.9.3, )",
"NetOfficeFw.OWC10Api": "[1.9.3, )",
"NetOfficeFw.Office": "[1.9.3, )",
"NetOfficeFw.VBIDE": "[1.9.3, )"
"NetOfficeFw.ADODBApi": "[2.0.0, )",
"NetOfficeFw.Core": "[2.0.0, )",
"NetOfficeFw.DAOApi": "[2.0.0, )",
"NetOfficeFw.MSComctlLibApi": "[2.0.0, )",
"NetOfficeFw.MSDATASRCApi": "[2.0.0, )",
"NetOfficeFw.OWC10Api": "[2.0.0, )",
"NetOfficeFw.Office": "[2.0.0, )",
"NetOfficeFw.VBIDE": "[2.0.0, )"
}
},
"NetOfficeFw.ADODBApi": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )"
"NetOfficeFw.Core": "[2.0.0, )"
}
},
"NetOfficeFw.Core": {
Expand All @@ -78,69 +78,75 @@
"NetOfficeFw.DAOApi": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )"
"NetOfficeFw.Core": "[2.0.0, )"
}
},
"NetOfficeFw.Excel": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.Office": "[1.9.3, )",
"NetOfficeFw.VBIDE": "[1.9.3, )"
"NetOfficeFw.Core": "[2.0.0, )",
"NetOfficeFw.Office": "[2.0.0, )",
"NetOfficeFw.VBIDE": "[2.0.0, )"
}
},
"NetOfficeFw.MSComctlLibApi": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.Core": "[2.0.0, )",
"stdole": "[7.0.3300, )"
}
},
"NetOfficeFw.MSDATASRCApi": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )"
"NetOfficeFw.Core": "[2.0.0, )"
}
},
"NetOfficeFw.Office": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.Core": "[2.0.0, )",
"stdole": "[7.0.3300, )"
}
},
"NetOfficeFw.Outlook": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.Office": "[1.9.3, )",
"NetOfficeFw.Core": "[2.0.0, )",
"NetOfficeFw.Office": "[2.0.0, )",
"stdole": "[7.0.3300, )"
}
},
"NetOfficeFw.OWC10Api": {
"type": "Project",
"dependencies": {
"NetOfficeFw.ADODBApi": "[1.9.3, )",
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.MSComctlLibApi": "[1.9.3, )",
"NetOfficeFw.MSDATASRCApi": "[1.9.3, )",
"NetOfficeFw.ADODBApi": "[2.0.0, )",
"NetOfficeFw.Core": "[2.0.0, )",
"NetOfficeFw.MSComctlLibApi": "[2.0.0, )",
"NetOfficeFw.MSDATASRCApi": "[2.0.0, )",
"stdole": "[7.0.3300, )"
}
},
"NetOfficeFw.PowerPoint": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.Office": "[1.9.3, )",
"NetOfficeFw.VBIDE": "[1.9.3, )",
"NetOfficeFw.Core": "[2.0.0, )",
"NetOfficeFw.Office": "[2.0.0, )",
"NetOfficeFw.VBIDE": "[2.0.0, )",
"stdole": "[7.0.3300, )"
}
},
"NetOfficeFw.VBIDE": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[1.9.3, )",
"NetOfficeFw.Office": "[1.9.3, )"
"NetOfficeFw.Core": "[2.0.0, )",
"NetOfficeFw.Office": "[2.0.0, )"
}
},
"noassemblytitle": {
"type": "Project",
"dependencies": {
"NetOfficeFw.Core": "[2.0.0, )"
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions Source/NetOffice.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetOffice.Tests", "NetOffic
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OfficeApi.Extensions", "Office.Extensions\OfficeApi.Extensions.csproj", "{A928079E-A92E-4964-A586-EFED61ED665A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F0235130-228E-419C-B21B-7F17E205C90A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NoAssemblyTitle", "..\Tests\NoAssemblyTitle\NoAssemblyTitle.csproj", "{5A6FBEE8-DF8A-4A52-8734-CE00289FCBB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -118,10 +122,17 @@ Global
{A928079E-A92E-4964-A586-EFED61ED665A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A928079E-A92E-4964-A586-EFED61ED665A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A928079E-A92E-4964-A586-EFED61ED665A}.Release|Any CPU.Build.0 = Release|Any CPU
{5A6FBEE8-DF8A-4A52-8734-CE00289FCBB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A6FBEE8-DF8A-4A52-8734-CE00289FCBB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A6FBEE8-DF8A-4A52-8734-CE00289FCBB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A6FBEE8-DF8A-4A52-8734-CE00289FCBB1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5A6FBEE8-DF8A-4A52-8734-CE00289FCBB1} = {F0235130-228E-419C-B21B-7F17E205C90A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {11306061-4126-4B70-AE48-BFAA3965B8A5}
EndGlobalSection
Expand Down
2 changes: 1 addition & 1 deletion Source/NetOffice/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public void Initialize(CacheOptions cacheOptions)

if (Settings.EnableMoreDebugOutput)
{
string localPath = Resolver.UriResolver.ResolveLocalPath(ThisAssembly.CodeBase);
string localPath = ThisAssembly.Location;
Console.WriteLine("Local Bind Path:{0}", localPath);
}

Expand Down
2 changes: 1 addition & 1 deletion Source/NetOffice/Diagnostics/SelfDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public string AssemblyTitle
if (titleAttribute.Title != String.Empty)
return titleAttribute.Title;
}
return System.IO.Path.GetFileNameWithoutExtension(OwnerAssembly.CodeBase);
return System.IO.Path.GetFileNameWithoutExtension(OwnerAssembly.Location);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/NetOffice/Loader/CurrentAppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal Assembly Load(AssemblyName name)
versionMatch = null == localPath ? ValidateVersion(name) : ValidateVersion(localPath);
if (null == localPath)
{
string thisLocalPath = Resolver.UriResolver.ResolveLocalPath(Owner.ThisAssembly.CodeBase);
string thisLocalPath = Owner.ThisAssembly.Location;
string extension = Path.GetExtension(thisLocalPath);
string path = Path.GetDirectoryName(thisLocalPath);
localPath = Path.Combine(path, name.Name + extension);
Expand Down Expand Up @@ -238,7 +238,7 @@ private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs a
if (args.Name.ContainsIgnoreCase(".resources"))
return null;

string thisLocalPath = Resolver.UriResolver.ResolveLocalPath(Owner.ThisAssembly.CodeBase);
string thisLocalPath = Owner.ThisAssembly.Location;
string extension = Path.GetExtension(thisLocalPath);
string path = Path.GetDirectoryName(thisLocalPath);
string fullFileName = Path.Combine(path, args.Name + extension);
Expand Down
8 changes: 4 additions & 4 deletions Source/NetOffice/Loader/PathBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ internal static class PathBuilder
/// <returns>resolved path</returns>
public static string BuildLocalPathFromDependentAssembly(DependentAssembly assembly)
{
string fileName = assembly.ParentAssembly.CodeBase.Substring(0, assembly.ParentAssembly.CodeBase.LastIndexOf("/")) + "/" + assembly.Name;
fileName = fileName.Replace("/", "\\").Substring(8);
return fileName;
string parentAssemblyDirectory = Path.GetDirectoryName(assembly.ParentAssembly.Location);
string localFilename = Path.Combine(parentAssemblyDirectory, assembly.Name);
return localFilename;
}

/// <summary>
Expand All @@ -27,7 +27,7 @@ public static string BuildLocalPathFromDependentAssembly(DependentAssembly assem
/// <returns>resolved path</returns>
public static string BuildLocalPathFromAssemblyFileName(Core factory, string assemblyName)
{
string localAssemblyPath = Resolver.UriResolver.ResolveLocalPath(factory.ThisAssembly.CodeBase);
string localAssemblyPath = Resolver.UriResolver.ResolveLocalPath(factory.ThisAssembly.Location);
string directoryName = System.IO.Path.GetDirectoryName(localAssemblyPath);
string fullFileName = System.IO.Path.Combine(directoryName, assemblyName);
return fullFileName;
Expand Down
3 changes: 3 additions & 0 deletions Source/NetOffice/Tools/COMAddinRegisterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public static void Proceed(Type type, string[] addinOfficeRegistryKey, InstallSc
{
Assembly thisAssembly = Assembly.GetAssembly(type);
string assemblyVersion = thisAssembly.GetName().Version.ToString();
#pragma warning disable SYSLIB0012 // Type or member is obsolete
// although `CodeBase` is obsolete, the COM registry value requires it
CodebaseAttribute.CreateValue(type.GUID, isSystemComponent, assemblyVersion, thisAssembly.CodeBase);
#pragma warning restore SYSLIB0012 // Type or member is obsolete
}
catch (Exception)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Office/Tools/Informations/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public virtual string AssemblyTitle
if (titleAttribute.Title != String.Empty)
return titleAttribute.Title;
}
return System.IO.Path.GetFileNameWithoutExtension(OwnerAssembly.CodeBase);
return System.IO.Path.GetFileNameWithoutExtension(OwnerAssembly.Location);
}
}

Expand Down
3 changes: 3 additions & 0 deletions Source/Word/Tools/DocumentInspectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ private static void RegisterHandleCodebase(Type type, InstallScope scope)
bool isSystemComponent = location.IsMachineComponentTarget(scope);
Assembly thisAssembly = Assembly.GetAssembly(type);
string assemblyVersion = thisAssembly.GetName().Version.ToString();
#pragma warning disable SYSLIB0012 // Type or member is obsolete
// although `CodeBase` is obsolete, the COM registry value requires it
CodebaseAttribute.CreateValue(type.GUID, isSystemComponent, assemblyVersion, thisAssembly.CodeBase);
#pragma warning restore SYSLIB0012 // Type or member is obsolete

}
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/NoAssemblyTitle/NoAssemblyTitle.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462</TargetFrameworks>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\Source\NetOffice.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\NetOffice\NetOffice.csproj" />
</ItemGroup>

</Project>
Loading
Loading