Skip to content

Commit 9ec3276

Browse files
fix: Ignore properties/methods with ComVisible=false (#217)
1 parent 12df665 commit 9ec3276

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

src/dscom/writer/InterfaceWriter.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System.Globalization;
16+
using System.Reflection;
1617
using System.Runtime.InteropServices;
1718

1819
namespace dSPACE.Runtime.InteropServices.Writer;
@@ -34,7 +35,7 @@ public InterfaceWriter(Type sourceType, LibraryWriter libraryWriter, WriterConte
3435

3536
public abstract Guid BaseInterfaceGuid { get; }
3637

37-
protected List<MethodWriter> MethodWriter { get; } = new();
38+
protected List<MethodWriter?> MethodWriters { get; } = new();
3839

3940
private ITypeInfo? BaseTypeInfo { get; set; }
4041

@@ -72,7 +73,7 @@ public override void Create()
7273
DispatchIdCreator.NormalizeIds();
7374

7475
// Create all writer.
75-
MethodWriter.ForEach(writer => writer.Create());
76+
MethodWriters.ForEach(writer => writer?.Create());
7677

7778
TypeInfo.LayOut().ThrowIfFailed($"Failed to layout type {SourceType}.");
7879
}
@@ -84,43 +85,54 @@ private void CreateMethodWriters()
8485

8586
foreach (var method in methods)
8687
{
87-
var numIdenticalNames = MethodWriter.Count(z => z.IsVisibleMethod && (z.MemberInfo.Name == method.Name || z.MethodName.StartsWith(method.Name + "_", StringComparison.Ordinal)));
88+
var numIdenticalNames = MethodWriters.Count(z => z is not null && z.IsVisibleMethod && (z.MemberInfo.Name == method.Name || z.MethodName.StartsWith(method.Name + "_", StringComparison.Ordinal)));
8889

8990
numIdenticalNames += GetMethodNamesOfBaseTypeInfo(BaseTypeInfo).Count(z => z == method.Name || z.StartsWith(method.Name + "_", StringComparison.Ordinal));
9091

9192
var alternateName = numIdenticalNames == 0 ? method.Name : method.Name + "_" + (numIdenticalNames + 1).ToString(CultureInfo.InvariantCulture);
9293
MethodWriter? methodWriter = null;
9394
if ((method.Name.StartsWith("get_", StringComparison.Ordinal) || method.Name.StartsWith("set_", StringComparison.Ordinal)) && method.IsSpecialName)
9495
{
95-
alternateName = alternateName.Substring(4);
96-
if (method.Name.StartsWith("get_", StringComparison.Ordinal))
97-
{
98-
methodWriter = new PropertyGetMethodWriter(this, method, Context, alternateName);
99-
}
100-
else
101-
if (method.Name.StartsWith("set_", StringComparison.Ordinal))
96+
var propertyInfo = method.DeclaringType!.GetProperties().First(p => p.GetGetMethod() == method || p.GetSetMethod() == method);
97+
var comVisibleAttribute = propertyInfo.GetCustomAttribute<ComVisibleAttribute>();
98+
99+
if (comVisibleAttribute is null || comVisibleAttribute.Value)
102100
{
103-
methodWriter = new PropertySetMethodWriter(this, method, Context, alternateName);
101+
alternateName = alternateName.Substring(4);
102+
if (method.Name.StartsWith("get_", StringComparison.Ordinal))
103+
{
104+
methodWriter = new PropertyGetMethodWriter(this, method, Context, alternateName);
105+
}
106+
else
107+
if (method.Name.StartsWith("set_", StringComparison.Ordinal))
108+
{
109+
methodWriter = new PropertySetMethodWriter(this, method, Context, alternateName);
110+
}
104111
}
105112
}
106113
else
107114
{
108-
methodWriter = new(this, method, Context, alternateName);
109-
}
110-
if (methodWriter != null)
111-
{
112-
MethodWriter.Add(methodWriter);
115+
var comVisibleAttribute = method.GetCustomAttribute<ComVisibleAttribute>();
116+
if (comVisibleAttribute is null || comVisibleAttribute.Value)
117+
{
118+
methodWriter = new(this, method, Context, alternateName);
119+
}
113120
}
121+
122+
MethodWriters.Add(methodWriter);
114123
}
115124

116125
var index = 0;
117126
var functionIndex = 0;
118-
foreach (var methodWriter in MethodWriter)
127+
foreach (var methodWriter in MethodWriters)
119128
{
120-
methodWriter.FunctionIndex = functionIndex;
121-
methodWriter.VTableOffset = VTableOffsetUserMethodStart + (index * IntPtr.Size);
122-
DispatchIdCreator!.RegisterMember(methodWriter);
123-
functionIndex += methodWriter.IsValid ? 1 : 0;
129+
if (methodWriter is not null)
130+
{
131+
methodWriter.FunctionIndex = functionIndex;
132+
methodWriter.VTableOffset = VTableOffsetUserMethodStart + (index * IntPtr.Size);
133+
DispatchIdCreator!.RegisterMember(methodWriter);
134+
functionIndex += methodWriter.IsValid ? 1 : 0;
135+
}
124136
index++;
125137
}
126138
}
@@ -163,10 +175,10 @@ private static IEnumerable<string> GetMethodNamesOfBaseTypeInfo(ITypeInfo? typeI
163175

164176
protected override void Dispose(bool disposing)
165177
{
166-
if (MethodWriter != null)
178+
if (MethodWriters != null)
167179
{
168-
MethodWriter.ForEach(t => t.Dispose());
169-
MethodWriter.Clear();
180+
MethodWriters.ForEach(t => t?.Dispose());
181+
MethodWriters.Clear();
170182
}
171183

172184
base.Dispose(disposing);

0 commit comments

Comments
 (0)