Skip to content

Commit

Permalink
refactor: null check and readonly props and fields
Browse files Browse the repository at this point in the history
  • Loading branch information
marklechtermann committed Jun 27, 2023
1 parent 3af45c2 commit baa0416
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 19 deletions.
15 changes: 11 additions & 4 deletions src/dscom.test/builder/DynamicBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ public DynamicBuilder(string name)

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

var attributeUsageAttribute = type.GetCustomAttribute<AttributeUsageAttribute>();
if (attributeUsageAttribute != null && !attributeUsageAttribute.ValidOn.HasFlag(AttributeTarget))
if (attributeUsageAttribute != null)
{
throw new ArgumentException($"Attribute {type.Name} not allowed here.");
if (!attributeUsageAttribute.ValidOn.HasFlag(AttributeTarget))
{
throw new ArgumentException($"Attribute {type.Name} not allowed here.");
}
}

var dispIDAttributeBuilder = new CustomAttributeBuilder(dispIDAttributeConstructor, values ?? Array.Empty<object>());
var dispIDAttributeBuilder = new CustomAttributeBuilder(dispIDAttributeConstructor!, values ?? Array.Empty<object>());
CustomAttributeBuilder.Add(dispIDAttributeBuilder);
return (T)this;
}
Expand Down
7 changes: 6 additions & 1 deletion src/dscom.test/builder/DynamicEnumBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ public DynamicEnumBuilder WithLiteralAndAttribute(string name, object? value, Ty
{
WithLiteral(name, value);

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

var attributeUsageAttribute = type.GetCustomAttribute<AttributeUsageAttribute>();
if (attributeUsageAttribute != null)
{
Expand Down
16 changes: 13 additions & 3 deletions src/dscom/RegistrationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ 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) ?? 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;
if (value == null)
{
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 @@ -189,7 +194,12 @@ public bool RegisterAssembly(Assembly assembly, bool registerCodeBase, ManagedCa
throw new ArgumentException("Cannot register a ReflectionOnly or dynamic assembly");
}

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

string? codeBase = null;
if (registerCodeBase && assembly.Location is null)

Check warning on line 204 in src/dscom/RegistrationServices.cs

View workflow job for this annotation

GitHub Actions / acceptance-test-msbuild (OutProc, outproc)

'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.

Check warning on line 204 in src/dscom/RegistrationServices.cs

View workflow job for this annotation

GitHub Actions / unit-test

'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.

Check warning on line 204 in src/dscom/RegistrationServices.cs

View workflow job for this annotation

GitHub Actions / unit-test

'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.

Check warning on line 204 in src/dscom/RegistrationServices.cs

View workflow job for this annotation

GitHub Actions / acceptance-test-msbuild (32 Bit, 32bit)

'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
{
Expand Down
6 changes: 5 additions & 1 deletion src/dscom/TypeInfoResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public TypeInfoResolver(WriterContext writerContext)
MajorVersion = 2,
MinorVersion = 0
};
var typeLibOle = GetTypeLibFromIdentifier(identifierOle) ?? throw new COMException("System.Guid not found in any type library");
var typeLibOle = GetTypeLibFromIdentifier(identifierOle);
if (typeLibOle == null)
{
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 readonly bool Equals(object? obj)
public override bool Equals(object? obj)
{
return obj is TypeLibIdentifier o && Equals(o);
}
Expand All @@ -63,7 +63,7 @@ public struct TypeLibIdentifier
/// </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 readonly bool Equals(TypeLibIdentifier other)
public bool Equals(TypeLibIdentifier other)
{
return MajorVersion == other.MajorVersion
&& MinorVersion == other.MinorVersion
Expand All @@ -75,7 +75,7 @@ public struct TypeLibIdentifier
/// A hash code for the current object.
/// </summary>
/// <returns>A hash code for the current object.</returns>
public override readonly int GetHashCode()
public override int GetHashCode()
{
return MajorVersion.GetHashCode() ^ MinorVersion.GetHashCode() ^ LibID.GetHashCode() ^ LanguageIdentifier.GetHashCode();
}
Expand Down
2 changes: 1 addition & 1 deletion src/dscom/internal/structs/CUSTDATA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct CUSTDATA

public IntPtr prgCustData;

public readonly CUSTDATAITEM[] Items
public CUSTDATAITEM[] Items
{
get
{
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 readonly bool Succeeded => _value >= 0;
public bool Succeeded => _value >= 0;

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

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

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

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

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

Please sign in to comment.