Skip to content

Commit

Permalink
refactor: code cleanup and documentation (#177)
Browse files Browse the repository at this point in the history
* refactor: class documentation
* refactor: null check 
* refactor: readonly props and fields
* refactor: code cleanup
  • Loading branch information
marklechtermann committed Jun 27, 2023
1 parent f45a337 commit 34c9848
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 43 deletions.
7 changes: 1 addition & 6 deletions src/dscom.test/builder/DynamicBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ public DynamicBuilder(string name)

public T WithCustomAttribute(Type type, Type[]? constructorParamTypes, object[]? values)
{
var dispIDAttributeConstructor = type.GetConstructor(constructorParamTypes ?? Array.Empty<Type>());
if (dispIDAttributeConstructor == null)
{
throw new ArgumentException($"Constructor for {type} not found");
}

var dispIDAttributeConstructor = type.GetConstructor(constructorParamTypes ?? Array.Empty<Type>()) ?? throw new ArgumentException($"Constructor for {type} not found");
var attributeUsageAttribute = type.GetCustomAttribute<AttributeUsageAttribute>();
if (attributeUsageAttribute != null)
{
Expand Down
7 changes: 1 addition & 6 deletions src/dscom.test/builder/DynamicEnumBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ public DynamicEnumBuilder WithLiteralAndAttribute(string name, object? value, Ty
{
WithLiteral(name, value);

var attributeConstructor = type.GetConstructor(constructorParamTypes ?? Array.Empty<Type>());
if (attributeConstructor == null)
{
throw new ArgumentException($"Constructor for {type} not found");
}

var attributeConstructor = type.GetConstructor(constructorParamTypes ?? Array.Empty<Type>()) ?? throw new ArgumentException($"Constructor for {type} not found");
var attributeUsageAttribute = type.GetCustomAttribute<AttributeUsageAttribute>();
if (attributeUsageAttribute != null)
{
Expand Down
3 changes: 3 additions & 0 deletions src/dscom/MarshalExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

namespace dSPACE.Runtime.InteropServices;

/// <summary>
/// Provides extension methods for marshaling data between managed and unmanaged code.
/// </summary>
internal static class MarshalExtension
{
[SuppressMessage("Microsoft.Style", "IDE1006", Justification = "")]
Expand Down
16 changes: 3 additions & 13 deletions src/dscom/RegistrationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,8 @@ public void RegisterTypeForComClients(Type type, ref Guid g)
/// <exception cref="T:System.ArgumentNullException">The <paramref name="type" /> parameter cannot be created.</exception>
public int RegisterTypeForComClients(Type type, ComTypes.RegistrationClassContext classContext, ComTypes.RegistrationConnectionType flags)
{
var value = type.GetCustomAttributes<GuidAttribute>().FirstOrDefault()?.Value
?? type.Assembly.GetCustomAttributes<GuidAttribute>().FirstOrDefault()?.Value;
if (value == null)
{
throw new ArgumentException($"The given type {type} does not have a valid GUID attribute.");
}

var value = (type.GetCustomAttributes<GuidAttribute>().FirstOrDefault()?.Value
?? type.Assembly.GetCustomAttributes<GuidAttribute>().FirstOrDefault()?.Value) ?? throw new ArgumentException($"The given type {type} does not have a valid GUID attribute.");
var guid = new Guid(value);

var genericClassFactory = typeof(ClassFactory<>);
Expand Down Expand Up @@ -194,12 +189,7 @@ public bool RegisterAssembly(Assembly assembly, bool registerCodeBase, ManagedCa
throw new ArgumentException("Cannot register a ReflectionOnly or dynamic assembly");
}

var fullName = assembly.FullName;
if (fullName is null)
{
throw new ArgumentException("Cannot register an assembly without a full name");
}

var fullName = assembly.FullName ?? throw new ArgumentException("Cannot register an assembly without a full name");
string? codeBase = null;
if (registerCodeBase && assembly.Location is null)
{
Expand Down
6 changes: 1 addition & 5 deletions src/dscom/TypeInfoResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ public TypeInfoResolver(WriterContext writerContext)
MajorVersion = 2,
MinorVersion = 0
};
var typeLibOle = GetTypeLibFromIdentifier(identifierOle);
if (typeLibOle == null)
{
throw new COMException("System.Guid not found in any type library");
}
var typeLibOle = GetTypeLibFromIdentifier(identifierOle) ?? throw new COMException("System.Guid not found in any type library");
typeLibOle.GetTypeInfo(0, out retval);
}
else if (type.GUID == new Guid(Guids.IID_IDispatch))
Expand Down
6 changes: 3 additions & 3 deletions src/dscom/TypeLibIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public struct TypeLibIdentifier
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object? obj)
public override readonly bool Equals(object? obj)
{
return obj is TypeLibIdentifier o && Equals(o);
}
Expand All @@ -63,7 +63,7 @@ public override bool Equals(object? obj)
/// </summary>
/// <param name="other">The <see cref="TypeLibIdentifier"/> to compare</param>
/// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
public bool Equals(TypeLibIdentifier other)
public readonly bool Equals(TypeLibIdentifier other)
{
return MajorVersion == other.MajorVersion
&& MinorVersion == other.MinorVersion
Expand All @@ -75,7 +75,7 @@ public bool Equals(TypeLibIdentifier other)
/// A hash code for the current object.
/// </summary>
/// <returns>A hash code for the current object.</returns>
public override int GetHashCode()
public override readonly int GetHashCode()
{
return MajorVersion.GetHashCode() ^ MinorVersion.GetHashCode() ^ LibID.GetHashCode() ^ LanguageIdentifier.GetHashCode();
}
Expand Down
10 changes: 5 additions & 5 deletions src/dscom/internal/structs/HRESULT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public struct HRESULT
[FieldOffset(0)]
private readonly int _value;

public bool Succeeded => _value >= 0;
public readonly bool Succeeded => _value >= 0;

public bool Failed => _value < 0;
public readonly bool Failed => _value < 0;

public HRESULT(int i)
{
Expand Down Expand Up @@ -78,15 +78,15 @@ public static explicit operator HRESULT(int value)
}

[SecurityCritical, SecuritySafeCritical]
public void ThrowIfFailed(string? message = null)
public readonly void ThrowIfFailed(string? message = null)
{
if (Failed)
{
throw new COMException(message, Marshal.GetExceptionForHR(_value));
}
}

public override bool Equals(object? obj)
public override readonly bool Equals(object? obj)
{
if (obj == null)
{
Expand All @@ -111,7 +111,7 @@ public override bool Equals(object? obj)
return false;
}

public override int GetHashCode()
public override readonly int GetHashCode()
{
return _value;
}
Expand Down
6 changes: 1 addition & 5 deletions src/dscom/writer/ClassWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ public override void CreateTypeInheritance()
{
foreach (var interfaceTypeValue in sourceInterfaceAttribute.Value.Split('\0').Distinct())
{
var interfaceType = SourceType.Assembly.GetType(interfaceTypeValue);
if (interfaceType == null)
{
interfaceType = AppDomain.CurrentDomain.GetAssemblies().Select(z => z.GetType(interfaceTypeValue)).FirstOrDefault(x => x != null);
}
var interfaceType = SourceType.Assembly.GetType(interfaceTypeValue) ?? AppDomain.CurrentDomain.GetAssemblies().Select(z => z.GetType(interfaceTypeValue)).FirstOrDefault(x => x != null);
if (interfaceType != null)
{
var interfaceTypeInfo = Context.TypeInfoResolver.ResolveTypeInfo(interfaceType);
Expand Down

0 comments on commit 34c9848

Please sign in to comment.