public void ResXFileRefToMemoryStream(string typeNameInResx) { using var env = TestEnvironment.Create(_output); var baseDir = env.CreateFolder(createFolder: true); var resourceHandlingFolder = baseDir.CreateDirectory("ResourceHandling"); var linkedTextFile = resourceHandlingFolder.CreateFile("FileToBeIncluded.txt"); File.WriteAllText(linkedTextFile.Path, "Test data"); var resources = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( $@" <data name='Image1' type='System.Resources.ResXFileRef, System.Windows.Forms'> <value>{linkedTextFile.Path};{typeNameInResx}</value> </data> ")); var resource = resources.ShouldHaveSingleItem() .ShouldBeOfType <LiveObjectResource>(); resource.Name.ShouldBe("Image1"); byte[] bytes = new byte[4]; resource.Value .ShouldBeOfType <MemoryStream>() .Read(bytes, 0, 4); bytes.ShouldBe(new byte[] { 84, 101, 115, 116 }, "Expected the bytes of 'Test' to start the stream"); }
public void PassesThroughBitmapInResx() { var resxWithEmbeddedBitmap = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @" <assembly alias=""System.Drawing"" name=""System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"" /> <data name=""pictureBox1.Image"" type=""System.Drawing.Bitmap, System.Drawing"" mimetype=""application/x-microsoft.net.object.bytearray.base64""> <value> iVBORw0KGgoAAAANSUhEUgAAACgAAAAeCAIAAADRv8uKAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO wwAADsMBx2+oZAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMS41ZEdYUgAAAZNJREFUSEvtk6GL wmAYxlfMFoNpQWe4Nqt/gIjBJAgiVoMIhomCiElMphUZGtRm1jTErSlcEAwaxei6eCDbPe4dY8g4dnc4 y37p+R5e9tu37xtjvIlA7BuB2DcCsW8E4lqt1ul0ut3ubDbTdZ3KarXabDYpHw6HSqVCGUyn01arhabX 61mVN57FDMMUi8V2u83zPMuyp9OJSnA+n5E3mw2yOWuUy2VkQRD6/X6pVFoul9R7wUWMR1POZrPYOpUc xzUaDeTtdkvi8XgciUQul8tj9Pf8JC4UCvV6ncrhcBgKha7X6263I3E6nabX+hsuYlmWj8ejKIrI6/Wa yv1+n8lkBoOBLY7FYpPJBCGXy32YrFarxyO84SIGON1UKmWfGRqIF4tFPB63xbgEo9EI4dMEpaIo5rgn XMT2p7ZBCR9CMpnEJSJxPp93Xu9XibEnBPxjyABZVVUESZLu9zvNvFAMotEoicF8Pk8kEliGw2Fc+3+J b7eblRw4yy8Ta2GCpaZp1sIzz2LfCMS+EYh9401iw/gG1gYfvzjQIXcAAAAASUVORK5CYII= </value> </data> ")); resxWithEmbeddedBitmap.ShouldHaveSingleItem(); resxWithEmbeddedBitmap[0].ShouldBeOfType(typeof(TypeConverterByteArrayResource)); var resource = (TypeConverterByteArrayResource)resxWithEmbeddedBitmap[0]; resource.Name.ShouldBe("pictureBox1.Image"); resource.TypeAssemblyQualifiedName.ShouldBe("System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); }
public void LoadsStringFromFileRefAsStringWithShiftJISEncoding() { using (var env = TestEnvironment.Create(_output)) { const string JapaneseString = "ハローワールド!"; var baseDir = env.CreateFolder(createFolder: true); var resourceHandlingFolder = baseDir.CreateDirectory("ResourceHandling"); var linkedTextFile = resourceHandlingFolder.CreateFile("TextFileInShiftJIS.txt"); #if RUNTIME_TYPE_NETCORE Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); #endif File.WriteAllText(linkedTextFile.Path, JapaneseString, Encoding.GetEncoding("shift_jis")); var resxWithLinkedString = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @" <assembly alias=""System.Windows.Forms"" name=""System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" /> <data name=""TextFile1"" type=""System.Resources.ResXFileRef, System.Windows.Forms""> <value>ResourceHandling\TextFileInShiftJIS.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;shift_jis</value> </data>"), Path.Combine(baseDir.Path, nameof(LoadsStringFromFileRefAsStringWithShiftJISEncoding) + ".resx"), useRelativePath: true); AssertSingleStringResource(resxWithLinkedString, "TextFile1", JapaneseString); } }
public void ParsesSingleStringWithPartialTypeName() { var resxWithSingleString = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @"<data name=""StringResource"" type=""System.String""> <value>StringValue</value> </data>")); AssertSingleStringResource(resxWithSingleString, "StringResource", "StringValue"); }
public void ParsesSingleWhitespaceStringWithNoPreserveAsEmptyString() { var resxWithSingleString = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @"<data name=""StringResource""> <value> </value> <comment>Comment</comment> </data>")); AssertSingleStringResource(resxWithSingleString, "StringResource", ""); }
public void ParsesSingleStringAsString() { var resxWithSingleString = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @"<data name=""StringResource"" xml:space=""preserve""> <value>StringValue</value> <comment>Comment</comment> </data>")); AssertSingleStringResource(resxWithSingleString, "StringResource", "StringValue"); }
public void Translate(string infile, string outfile) { var writer = new System.Resources.ResourceWriter(outfile); foreach (var resource in MSBuildResXReader.GetResourcesFromFile(infile, pathsRelativeToBasePath: true)) { resource.AddTo(writer); } writer.Generate(); writer.Close(); }
public void LoadsStringFromFileRefAsString(string stringType) { File.Exists(Path.Combine("ResourceHandling", "TextFile1.txt")).ShouldBeTrue("Test deployment is missing None files"); var resxWithLinkedString = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( $@" <assembly alias=""System.Windows.Forms"" name=""System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" /> <data name=""TextFile1"" type=""System.Resources.ResXFileRef, System.Windows.Forms""> <value>ResourceHandling\TextFile1.txt;{stringType};utf-8</value> </data>")); AssertSingleStringResource(resxWithLinkedString, "TextFile1", "Contents of TextFile1"); }
public void ResXNullRefProducesNullLiveObject() { var resxWithNullRef = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @" <assembly alias=""System.Windows.Forms"" name=""System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" /> <data name=""$this.AccessibleDescription"" type=""System.Resources.ResXNullRef, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""> <value /> </data>")); resxWithNullRef.ShouldHaveSingleItem(); resxWithNullRef[0].Name.ShouldBe("$this.AccessibleDescription"); resxWithNullRef[0].ShouldBeOfType <LiveObjectResource>() .Value.ShouldBeNull(); }
public void AssemblyElementWithNoAliasInfersSimpleName() { var resxWithEmbeddedBitmap = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @" <assembly name=""System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"" /> <data name=""Color1"" type=""System.Drawing.Color, System.Drawing""><value>Blue</value></data> ")); resxWithEmbeddedBitmap.ShouldHaveSingleItem(); resxWithEmbeddedBitmap[0].ShouldBeOfType(typeof(TypeConverterStringResource)); var resource = (TypeConverterStringResource)resxWithEmbeddedBitmap[0]; resource.Name.ShouldBe("Color1"); resource.TypeAssemblyQualifiedName.ShouldBe("System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); resource.StringRepresentation.ShouldBe("Blue"); }
public void ResXFileRefToBitmap() { string bitmapPath = Build.UnitTests.GenerateResource_Tests.Utilities.CreateWorldsSmallestBitmap(); var resxWithLinkedBitmap = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( $@" <data name='Image1' type='System.Resources.ResXFileRef, System.Windows.Forms'> <value>{bitmapPath};System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> </data> ")); resxWithLinkedBitmap.ShouldHaveSingleItem(); resxWithLinkedBitmap[0].ShouldBeOfType(typeof(FileStreamResource)); var resource = (FileStreamResource)resxWithLinkedBitmap[0]; resource.Name.ShouldBe("Image1"); resource.TypeAssemblyQualifiedName.ShouldBe("System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); }
public void LoadsMultipleStringsPreservingOrder() { var resxWithTwoStrings = MSBuildResXReader.GetResourcesFromString( ResXHelper.SurroundWithBoilerplate( @"<data name=""StringResource"" xml:space=""preserve""> <value>StringValue</value> <comment>Comment</comment> </data> <data name=""2StringResource2"" xml:space=""preserve""> <value>2StringValue2</value> </data>")); resxWithTwoStrings.Count.ShouldBe(2); resxWithTwoStrings[0].Name.ShouldBe("StringResource"); resxWithTwoStrings[0].ShouldBeOfType <StringResource>() .Value.ShouldBe("StringValue"); resxWithTwoStrings[1].Name.ShouldBe("2StringResource2"); resxWithTwoStrings[1].ShouldBeOfType <StringResource>() .Value.ShouldBe("2StringValue2"); }
private static bool DefineResourceFetchingProperty(String propertyName, String resourceName, ResourceData data, CodeTypeDeclaration srClass, bool internalClass, bool useStatic) { CodeTypeReference valueType; bool isString; bool isStream; if (data.Type is Type type) { // We have an actual object on which we can do type comparisons if (type == typeof(MemoryStream)) { type = typeof(UnmanagedMemoryStream); } // Ensure type is internalally visible. This is necessary to ensure // users can access classes via a base type. Imagine a class like // Image or Stream as a internalally available base class, then an // internal type like MyBitmap or __UnmanagedMemoryStream as an // internal implementation for that base class. For internalally // available strongly typed resource classes, we must return the // internal type. For simplicity, we'll do that for internal strongly // typed resource classes as well. Ideally we'd also like to check // for interfaces like IList, but I don't know how to do that without // special casing collection interfaces & ignoring serialization // interfaces or IDisposable. while (!type.IsPublic) { type = type.BaseType; } valueType = new CodeTypeReference(type); isString = type == typeof(String); isStream = type == typeof(UnmanagedMemoryStream) || type == typeof(MemoryStream); } else { // We don't have access to a live copy of the object, so // we must do what we can with string information. // TODO: can we do the type gymnastics here, too? valueType = new CodeTypeReference(data.TypeFullName); isString = MSBuildResXReader.IsString(data.TypeAssemblyQualifiedName); isStream = MSBuildResXReader.IsMemoryStream(data.TypeAssemblyQualifiedName); } var prop = new CodeMemberProperty { Name = propertyName, HasGet = true, HasSet = false }; prop.Type = valueType; if (internalClass) { prop.Attributes = MemberAttributes.Assembly; } else { prop.Attributes = MemberAttributes.Public; } if (useStatic) { prop.Attributes |= MemberAttributes.Static; } // For Strings, emit this: // return ResourceManager.GetString("name", _resCulture); // For Streams, emit this: // return ResourceManager.GetStream("name", _resCulture); // For Objects, emit this: // Object obj = ResourceManager.GetObject("name", _resCulture); // return (MyValueType) obj; var resMgr = new CodePropertyReferenceExpression(null, "ResourceManager"); var resCultureField = new CodeFieldReferenceExpression((useStatic) ? null : new CodeThisReferenceExpression(), CultureInfoFieldName); String getMethodName; String text; String valueAsString = TruncateAndFormatCommentStringForOutput(data.ValueAsString); String typeName = String.Empty; if (!isString) // Stream or Object { typeName = TruncateAndFormatCommentStringForOutput(data.TypeFullName); } if (isString) { getMethodName = "GetString"; } else if (isStream) { getMethodName = "GetStream"; } else { getMethodName = "GetObject"; } if (isString) { text = SR.GetString(SR.StringPropertyComment, valueAsString); } else { // Stream or Object if (valueAsString == null || String.Equals(typeName, valueAsString)) // If the type did not override ToString, ToString just returns the type name. { text = SR.GetString(SR.NonStringPropertyComment, typeName); } else { text = SR.GetString(SR.NonStringPropertyDetailedComment, typeName, valueAsString); } } prop.Comments.Add(new CodeCommentStatement(DocCommentSummaryStart, true)); prop.Comments.Add(new CodeCommentStatement(text, true)); prop.Comments.Add(new CodeCommentStatement(DocCommentSummaryEnd, true)); var getValue = new CodeMethodInvokeExpression(resMgr, getMethodName, new CodePrimitiveExpression(resourceName), resCultureField); CodeMethodReturnStatement ret; if (isString || isStream) { ret = new CodeMethodReturnStatement(getValue); } else { var returnObj = new CodeVariableDeclarationStatement(typeof(Object), "obj", getValue); prop.GetStatements.Add(returnObj); ret = new CodeMethodReturnStatement(new CodeCastExpression(valueType, new CodeVariableReferenceExpression("obj"))); } prop.GetStatements.Add(ret); srClass.Members.Add(prop); return(true); }