Skip to content

Commit 88b6f04

Browse files
authored
Merge pull request #2 from Renilac/dev_handle_unsupported_types
don't overwrite already existing files better warning for incompatible types
2 parents b8dcb5a + a8e7f9e commit 88b6f04

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ http://www.codeproject.com/Articles/18954/Interop-Forms-Toolkit-Tutorial
1212
https://www.microsoft.com/en-us/download/details.aspx?id=3264
1313

1414
https://interoptoolkitcs.codeplex.com (also imported to https://github.com/froque/interoptoolkitcs)
15+
16+
### Info
17+
At present state this tool does not override files previously generated.
18+
When forms with unsupported parameter types are found, the initialize method will be generated with a paramenter with the root type "Object".
19+
20+
### How to Debug
21+
22+
Open project properties
23+
- Start Action
24+
- choose "Start external program"
25+
- and write path to visual studio. (Ex: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe")
26+
- On Command line arguments "/rootsuffix Exp"
27+
- Start application
28+
- Create project in newly opened VS window
29+
- Run tool

VSIXInteropFormsToolkit/GenerateInteropFormCommand.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ internal sealed class GenerateInteropFormCommand
3636
/// </summary>
3737
private readonly Package package;
3838

39+
/// <summary>
40+
/// Warning message presented flag for unsupported parameter types
41+
/// </summary>
42+
private bool DisplayWarningPresented = false;
43+
3944
/// <summary>
4045
/// Initializes a new instance of the <see cref="GenerateInteropFormCommand"/> class.
4146
/// Adds our command handlers for menu (commands must exist in the command table file)
@@ -164,6 +169,11 @@ private void CreateInteropFormProxiesForProject(Project currentAssembly, Project
164169
private void CreateInteropFormProxiesForDocument(List<CodeClass> interopFormClasses, Project currentAssembly, ProjectItem interopFormDoc)
165170
{
166171

172+
if (checkFileExists(currentAssembly, interopFormDoc))
173+
{
174+
return;
175+
}
176+
167177
if (interopFormClasses.Count <= 0)
168178
return;
169179

@@ -173,6 +183,7 @@ private void CreateInteropFormProxiesForDocument(List<CodeClass> interopFormClas
173183
namespace1.Name = "Interop";
174184
unit1.Namespaces.Add(namespace1);
175185
namespace1.Imports.Add(import1);
186+
namespace1.Imports.Add(new CodeNamespaceImport("System"));
176187
foreach (CodeClass class1 in interopFormClasses)
177188
{
178189
string text2 = class1.FullName;
@@ -258,9 +269,9 @@ private void CreateInteropFormProxiesForDocument(List<CodeClass> interopFormClas
258269
declaration1.CustomAttributes.Add(new CodeAttributeDeclaration("System.Runtime.InteropServices.ComSourceInterfaces", new System.CodeDom.CodeAttributeArgument[] { new System.CodeDom.CodeAttributeArgument(new CodeTypeOfExpression(declaration2.Name)) }));
259270
}
260271
}
261-
262-
272+
263273
ProjectItem generatedFolderItem = GetGeneratedFolderItem(currentAssembly);
274+
264275
FileInfo generatedItemInfo = getGeneratedItem(currentAssembly, generatedFolderItem, interopFormDoc);
265276

266277
StreamWriter writer1 = new StreamWriter(generatedItemInfo.Create());
@@ -285,7 +296,7 @@ private FileInfo getGeneratedItem(Project currentAssembly, ProjectItem generated
285296
{
286297
d.Create();
287298
}
288-
299+
289300
FileInfo infoGeneratedItem = new FileInfo(fullName);
290301
foreach (ProjectItem item in generatedFolderItem.ProjectItems)
291302
{
@@ -382,12 +393,18 @@ private void AddInitializeMethodForConstructor(CodeTypeDeclaration proxyClass, C
382393
bool flag1 = false;
383394
foreach (CodeParameter parameter1 in method.Parameters)
384395
{
396+
var paramType = parameter1.Type.AsFullName;
385397
if (!this.IsSupported(parameter1.Type))
386398
{
387-
this.DisplayWarning(String.Format(Resource.InitMethodErrMsg, parameter1.Type.AsFullName, parameter1.Name, parameter1.Type.AsFullName));
388-
return;
399+
if(!DisplayWarningPresented)
400+
{
401+
this.DisplayWarning(String.Format(Resource.InitMethodWarningMsg));
402+
DisplayWarningPresented = true;
403+
}
404+
paramType = "Object";
405+
389406
}
390-
CodeParameterDeclarationExpression expression1 = new CodeParameterDeclarationExpression(parameter1.Type.AsFullName, parameter1.Name);
407+
CodeParameterDeclarationExpression expression1 = new CodeParameterDeclarationExpression(paramType, parameter1.Name);
391408
method1.Parameters.Add(expression1);
392409
if (flag1)
393410
{
@@ -712,6 +729,24 @@ private List<CodeClass> GetInteropFormClasses(Project assemblyProj, ProjectItem
712729
return list2;
713730
}
714731

732+
private bool checkFileExists(Project currentAssembly, ProjectItem interopFormDoc)
733+
{
734+
FileInfo infoProjectItem = new FileInfo(interopFormDoc.get_FileNames(0));
735+
string name = infoProjectItem.Name.Replace(infoProjectItem.Extension, ".wrapper" + infoProjectItem.Extension);
736+
string folder = Path.GetDirectoryName(infoProjectItem.FullName.Replace(Path.GetDirectoryName(currentAssembly.FullName), Path.GetDirectoryName(currentAssembly.FullName)
737+
+ @"\" + Resource.INTEROP_FORM_PROXY_FOLDER_NAME));
738+
string fullName = folder + @"\" + name;
739+
740+
if (File.Exists(fullName))
741+
{
742+
return true;
743+
}
744+
else
745+
{
746+
return false;
747+
}
748+
}
749+
715750
private DTE2 _applicationObject;
716751
private System.Type _attrTypeEvent;
717752
private System.Type _attrTypeForm;

VSIXInteropFormsToolkit/Resource.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VSIXInteropFormsToolkit/Resource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,7 @@
174174
<data name="PropertyErrMsg" xml:space="preserve">
175175
<value>Type {0} is not supported. Property {1} will not be generated.</value>
176176
</data>
177+
<data name="InitMethodWarningMsg" xml:space="preserve">
178+
<value>Parameters with unsupported types found for initialize method, type Object will be used instead.</value>
179+
</data>
177180
</root>

0 commit comments

Comments
 (0)