public void ReadMobDataFromJson() { var unit = new CodeCompileUnit(); unit.Namespaces.Add(new CodeNamespace { Imports = { new CodeNamespaceImport("MiNET.Entities.Behaviors") } }); var ns = new CodeNamespace("MiNET.Generated"); ns.Comments.Add(new CodeCommentStatement("Types generated from bedrock component JSON")); unit.Namespaces.Add(ns); var files = Directory.EnumerateFileSystemEntries(@"D:\Downloads\bedrock-server-1.11.4.2\behavior_packs\vanilla\entities\", "*.json"); foreach (var file in files) { CreateEntity(ns, file); } CodeTypeDeclarationCollection types = ns.Types; CodeTypeDeclaration[] t = new CodeTypeDeclaration[types.Count]; types.CopyTo(t, 0); ns.Types.Clear(); var g = t.GroupBy(ctd => ctd.Name); foreach (var gg in g) { ns.Types.Add(gg.First()); } GenerateCSharpCode(unit); }
/// <summary> /// Generates CodeDom model of the source code based on XML schema. /// </summary> /// <param name="schemas">A set of compiled XML schemas.</param> /// <returns>CodeDom model of the gererated source code.</returns> private CodeCompileUnit GenerateCodeDom(XmlSchemas schemas) { CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); CodeNamespace @namespace = new CodeNamespace(string.Empty); codeCompileUnit.Namespaces.Add(@namespace); CodeGenerationOptions options = CodeGenerationOptions.None; XmlCodeExporter codeExporter = new XmlCodeExporter(@namespace, codeCompileUnit, this.LanguageProvider, options, null); XmlSchemaImporter schemaImporter = new XmlSchemaImporter(schemas, options, this.LanguageProvider, new ImportContext(new CodeIdentifiers(), false)); foreach (XmlSchema schema in schemas) { ImportSchemaAsClasses(schema, schemaImporter, codeExporter, @namespace); } CodeTypeDeclarationCollection types = @namespace.Types; if (types == null || types.Count == 0) { Console.WriteLine("No Classes Generated"); } CodeGenerator.ValidateIdentifiers(@namespace); return(codeCompileUnit); }
public void PerformRename(CodeTypeDeclarationCollection col) { if (col == null) { throw new ArgumentNullException("col"); } Dictionary <string, string> map = new Dictionary <string, string>(StringComparer.Ordinal); map.Add("tagPOINT", "Point"); CodeDomIterator it = new CodeDomIterator(); List <object> list = it.Iterate(col); // Use the iterator so we make sure to reach nested types foreach (CodeTypeDeclaration ctd in FindUnmodifiedTypes(list)) { NativeDefinedType definedNt = GetDefined(ctd); if (IsBadName(ctd.Name)) { foreach (NativeTypeDef possible in FindTypeDefsTargeting(definedNt)) { if (!IsBadName(possible.Name)) { map[ctd.Name] = possible.Name; } } } } SmartTypeRename(map, list); ResetCustomExpressions(list); }
private void Emit(Dictionary <string, string> usedClassName, string namespaceName, List <GlobalItem> items) { // it is a valid scenario for namespaceName to be empty //string namespaceName = Generator.SourceObjectNamespaceName; Generator.SourceEdmNamespaceName = namespaceName; // emit the namespace definition CodeNamespace codeNamespace = new CodeNamespace(namespaceName); // output some boiler plate comments string comments = Strings.NamespaceComments( System.IO.Path.GetFileName(_targetFilePath), DateTime.Now.ToString(System.Globalization.CultureInfo.CurrentCulture)); CommentEmitter.EmitComments(CommentEmitter.GetFormattedLines(comments, false), codeNamespace.Comments, false); CompileUnit.Namespaces.Add(codeNamespace); // Emit the classes in the schema foreach (GlobalItem element in items) { if (AddElementNameToCache(element, usedClassName)) { SchemaTypeEmitter emitter = CreateElementEmitter(element); CodeTypeDeclarationCollection typeDeclaration = emitter.EmitApiClass(); if (typeDeclaration.Count > 0) { codeNamespace.Types.AddRange(typeDeclaration); } } } Generator.SourceEdmNamespaceName = null; }
private static CodeTypeDeclarationCollection CollectionBuilder(string className, string classType, string ns) { // Declare a generic class. CodeTypeDeclaration newClass = new CodeTypeDeclaration(); newClass.Name = className; newClass.IsPartial = true; newClass.BaseTypes.Add(new CodeTypeReference("System.Collections.Generic.List", new CodeTypeReference[] { new CodeTypeReference(classType) })); // add the Serializable attribute to the class CodeAttributeDeclaration attribute = new CodeAttributeDeclaration("System.SerializableAttribute"); newClass.CustomAttributes.Add(attribute); // add the Serializable attribute to the class CodeAttributeDeclaration xmlTypeAttribute = new CodeAttributeDeclaration("System.Xml.Serialization.XmlTypeAttribute"); xmlTypeAttribute.Arguments.Add(new CodeAttributeArgument() { Name = "Namespace", Value = new CodePrimitiveExpression(ns) }); newClass.CustomAttributes.Add(xmlTypeAttribute); CodeTypeDeclarationCollection decls = new CodeTypeDeclarationCollection(); decls.Add(newClass); return(decls); }
static CodeTypeDeclaration[] _ToArray(CodeTypeDeclarationCollection refs) { var result = new CodeTypeDeclaration[refs.Count]; refs.CopyTo(result, 0); return(result); }
public void ShouldGenerateWithDefaultSerializer() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(false); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Company.xsd")); Assert.AreEqual <int>(4, types.Count); }
public void ShouldGenerateWithDataSetSchemaAndImportXmlType() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\DataSetSchema.xsd")); Assert.AreEqual <int>(2, types.Count); }
public void ShouldGeneratedWithIncludesAndXmlSerializer() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Company.xsd")); Assert.IsNotNull(types, "No exception thrown"); }
public void ShouldGenerateWithImporter() { WsdlImporter importer = DescriptionModelHelper.CreateImporter(@"SampleData\DescriptionModel\MockService.wsdl"); CodeTypeDeclarationCollection types = generator.GenerateTypes(importer); Assert.AreEqual <int>(1, types.Count); }
private static void SortAsc(CodeTypeDeclarationCollection TypeCollection) { List <CodeTypeDeclaration> codeTypes = new List <CodeTypeDeclaration>(); codeTypes.AddRange(TypeCollection.OfType <CodeTypeDeclaration>().ToArray()); CodeTypeDeclaration generatedType = codeTypes.FirstOrDefault( type => { if (type.UserData[Constants.GENERATED_TYPE] as string != null) { return(true); } return(false); }); if (generatedType != null) { codeTypes.Remove(generatedType); } //Sort Types Ascending By Type Name codeTypes.Sort((a, b) => { return(String.Compare(a.Name, b.Name)); }); TypeCollection.Clear(); TypeCollection.AddRange(codeTypes.ToArray()); if (generatedType != null) { TypeCollection.Add(generatedType); } }
static CodeTypeDeclarationCollection GenerateLongTree(string name, SupportedType[] fieldTypes, SupportedType[] propTypes, bool isEvent, bool isType2) { CodeTypeDeclarationCollection instrumentedTypes = new CodeTypeDeclarationCollection(); CodeTypeDeclaration type, typeD, typeDD, typeDDD, typeDDDD, typeDDDDD, typeDDDDDD; type = GenerateBigType(name + "A", null, fieldTypes, propTypes, isEvent ? SupportedType.attrProvEvent:SupportedType.attrProvAbstract); if (isType2) { type.BaseTypes.Add(isEvent?typeof(System.Management.Instrumentation.Event):typeof(System.Management.Instrumentation.Instance)); } typeD = GenerateBigType(name + "AA", type, fieldTypes, propTypes, isEvent ? SupportedType.attrProvEvent:SupportedType.attrProvAbstract); typeDD = GenerateBigType(name + "AAA", typeD, fieldTypes, propTypes, isEvent ? SupportedType.attrProvEvent:SupportedType.attrProvAbstract); typeDDD = GenerateBigType(name + "AAAA", typeDD, fieldTypes, propTypes, isEvent ? SupportedType.attrProvEvent:SupportedType.attrProvAbstract); typeDDDD = GenerateBigType(name + "AAAAA", typeDDD, fieldTypes, propTypes, isEvent ? SupportedType.attrProvEvent:SupportedType.attrProvAbstract); typeDDDDD = GenerateBigType(name + "AAAAAA", typeDDDD, fieldTypes, propTypes, isEvent ? SupportedType.attrProvEvent:SupportedType.attrProvAbstract); typeDDDDDD = GenerateBigType(name + "AAAAAAA", typeDDDDD, fieldTypes, propTypes, isEvent ? SupportedType.attrProvEvent:SupportedType.attrProvInstance); instrumentedTypes.Add(type); instrumentedTypes.Add(typeD); instrumentedTypes.Add(typeDD); instrumentedTypes.Add(typeDDD); instrumentedTypes.Add(typeDDDD); instrumentedTypes.Add(typeDDDDD); instrumentedTypes.Add(typeDDDDDD); return(instrumentedTypes); }
public override void Execute(CodeNamespace codeNamespace) { CodeTypeDeclarationCollection typesToRemove = new CodeTypeDeclarationCollection(); // foreach datatype in the codeNamespace foreach (CodeTypeDeclaration type in codeNamespace.Types) { if (Options.Type.Find(x => x.Name == codeNamespace.Name + "." + type.Name) != null || Options.Type.Find(x => x.Name == type.Name) != null) { typesToRemove.Add(type); } } foreach (CodeTypeDeclaration type in typesToRemove) { List <CodeAttributeDeclaration> toRemove = new List <CodeAttributeDeclaration>(); foreach (CodeAttributeDeclaration decl in type.CustomAttributes) { if (decl.Name == "System.Xml.Serialization.XmlTypeAttribute") { toRemove.Add(decl); } } foreach (var x in toRemove) { type.CustomAttributes.Remove(x); } //if (type.CustomAttributes.Contains(new CodeAttributeDeclaration("System.Xml.Serialization.XmlTypeAttribute"))) // type.CustomAttributes.Remove(new CodeAttributeDeclaration("System.Xml.Serialization.XmlTypeAttribute")); } }
/// <summary> /// Visits a <see cref="CodeTypeDeclarationCollection"/>. /// </summary> /// <param name="codeTypeDeclarationCollection">The <see cref="CodeTypeDeclarationCollection"/> to visit.</param> protected override void VisitCodeTypeDeclarationCollection(CodeTypeDeclarationCollection codeTypeDeclarationCollection) { CodeTypeDeclaration[] sortedTypeDeclarations = codeTypeDeclarationCollection.Cast <CodeTypeDeclaration>().OrderBy(c => c.Name).ToArray(); codeTypeDeclarationCollection.Clear(); codeTypeDeclarationCollection.AddRange(sortedTypeDeclarations); base.VisitCodeTypeDeclarationCollection(codeTypeDeclarationCollection); }
private List <object> Convert(string code) { BasicConverter converter = new BasicConverter(LanguageType.VisualBasic, StorageFactory.CreateStandard()); CodeTypeDeclarationCollection ctd = converter.ConvertNativeCodeToCodeDom(code, new PInvoke.ErrorProvider()); CodeDomIterator it = new CodeDomIterator(); return(it.Iterate(ctd)); }
private static void AddEntityOptionSetEnumDeclaration(CodeTypeDeclarationCollection types) { var enumClass = new CodeTypeDeclaration("EntityOptionSetEnum") { IsClass = true, TypeAttributes = TypeAttributes.Sealed | TypeAttributes.NotPublic, }; // public static int? GetEnum(Microsoft.Xrm.Sdk.Entity entity, string attributeLogicalName) var get = new CodeMemberMethod { Name = "GetEnum", ReturnType = new CodeTypeReference(typeof(int?)), // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags Attributes = System.CodeDom.MemberAttributes.Static | System.CodeDom.MemberAttributes.Public, }; get.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Microsoft.Xrm.Sdk.Entity), "entity")); get.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "attributeLogicalName")); // entity.Attributes.ContainsKey(attributeLogicalName) var entityAttributesContainsKey = new CodeMethodReferenceExpression( new CodePropertyReferenceExpression( new CodeArgumentReferenceExpression("entity"), "Attributes"), "ContainsKey"); var invokeContainsKey = new CodeMethodInvokeExpression(entityAttributesContainsKey, new CodeArgumentReferenceExpression("attributeLogicalName")); // Microsoft.Xrm.Sdk.OptionSetValue value = entity.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>(attributeLogicalName).Value; var declareAndSetValue = new CodeVariableDeclarationStatement { Type = new CodeTypeReference(typeof(OptionSetValue)), Name = "value", InitExpression = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( new CodeArgumentReferenceExpression("entity"), "GetAttributeValue", new CodeTypeReference(typeof(OptionSetValue))), new CodeArgumentReferenceExpression("attributeLogicalName")) }; // value != null var valueNeNull = new CodeSnippetExpression("value != null"); // value.Value var invokeValueGetValue = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("value"), "Value"); // if(invokeContainsKey){return invokeGetAttributeValue;}else{return null} get.Statements.Add(new CodeConditionStatement(invokeContainsKey, declareAndSetValue, new CodeConditionStatement(valueNeNull, new CodeMethodReturnStatement(invokeValueGetValue)))); // return null; get.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(null))); enumClass.Members.Add(get); types.Add(enumClass); }
public void ShouldGenerateEnumType() { CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Enums.xsd")); Assert.AreEqual <int>(1, types.Count); Assert.IsTrue(types[0].IsEnum); Assert.AreEqual <int>(4, types[0].Members.Count); }
static CodeTypeDeclarationCollection GenerateMultiTree(string name, SupportedType[] supportedTypes, bool isEvent, bool isType2) { CodeTypeDeclarationCollection types = new CodeTypeDeclarationCollection(); types.AddRange(GenerateTree(name + "F_", supportedTypes, null, isEvent, isType2)); types.AddRange(GenerateTree(name + "_P", null, supportedTypes, isEvent, isType2)); types.AddRange(GenerateTree(name + "FP", supportedTypes, supportedTypes, isEvent, isType2)); return(types); }
private static CodeTypeDeclarationCollection ConvertToCodeDom(string code) { ErrorProvider ep = new ErrorProvider(); BasicConverter con = new BasicConverter(LanguageType.VisualBasic, StorageFactory.CreateStandard()); CodeTypeDeclarationCollection result = con.ConvertNativeCodeToCodeDom(code, ep); Assert.Equal(0, ep.Errors.Count); return(result); }
public static void VerifyConstValue(LanguageType lang, NativeSymbolBag bag, string name, string val, string type) { Assert.True(bag.TryResolveSymbolsAndValues()); BasicConverter con = new BasicConverter(lang, StorageFactory.CreateStandard()); CodeTypeDeclarationCollection col = con.ConvertToCodeDom(bag, new ErrorProvider()); VerifyConstValue(col, lang, name, val, type); }
public void AddRange_Self() { CodeTypeDeclarationCollection coll = new CodeTypeDeclarationCollection(); coll.Add(new CodeTypeDeclaration()); Assert.AreEqual(1, coll.Count, "#1"); coll.AddRange(coll); Assert.AreEqual(2, coll.Count, "#2"); }
public void Constructor1_NullItem() { CodeTypeDeclaration[] declarations = new CodeTypeDeclaration[] { new CodeTypeDeclaration(), null }; CodeTypeDeclarationCollection coll = new CodeTypeDeclarationCollection( declarations); }
public void Constructor0() { CodeTypeDeclarationCollection coll = new CodeTypeDeclarationCollection(); Assert.IsFalse(((IList)coll).IsFixedSize, "#1"); Assert.IsFalse(((IList)coll).IsReadOnly, "#2"); Assert.AreEqual(0, coll.Count, "#3"); Assert.IsFalse(((ICollection)coll).IsSynchronized, "#4"); Assert.IsNotNull(((ICollection)coll).SyncRoot, "#5"); }
internal static void AddDelegate(CodeTypeDeclarationCollection codeClasses, string handlerType, string handlerArgs) { CodeTypeDelegate delegate2 = new CodeTypeDelegate(handlerType); delegate2.CustomAttributes.Add(GeneratedCodeAttribute); delegate2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "sender")); delegate2.Parameters.Add(new CodeParameterDeclarationExpression(handlerArgs, "e")); delegate2.Comments.Add(new CodeCommentStatement(Res.GetString("CodeRemarks"), true)); codeClasses.Add(delegate2); }
public void SetUp() { buildData = new BuildDataDictionary(); this.typeDeclarations = new CodeTypeDeclarationCollection(); this.testClassDeclaration = new System.CodeDom.CodeTypeDeclaration(); typeDeclarations.Add(this.testClassDeclaration); this.namespaceDetector = new NStub.CSharp.NamespaceDetector(typeDeclarations); this.testObject = new CodeTypeSetup(this.namespaceDetector, buildData, this.testClassDeclaration); }
private void IterateTypeDeclarationImpl(CodeTypeDeclarationCollection col, List <object> list) { ThrowIfNull(col); ThrowIfNull(list); foreach (CodeTypeDeclaration ctd in col) { IterateTypeMemberImpl(ctd, list); } }
static CodeStatementCollection GenTestCases(CodeTypeDeclarationCollection types) { CodeStatementCollection statements = new CodeStatementCollection(); foreach (CodeTypeDeclaration typeDef in types) { statements.AddRange(TestCreateInstance(typeDef, typeDef.Name.ToLower())); } return(statements); }
/// <summary> /// Core conversion routine. All code should just go through this /// </summary> /// <param name="bag"></param> /// <param name="ep"></param> /// <returns></returns> /// <remarks></remarks> private CodeTypeDeclarationCollection ConvertBagToCodeDom(NativeSymbolBag bag, ErrorProvider ep) { ThrowIfNull(bag); ThrowIfNull(ep); // Make sure than all of the referenced NativeDefinedType instances are in the correct // portion of the bag ChaseReferencedDefinedTypes(bag); // First step is to resolve the symbols bag.TryResolveSymbolsAndValues(ep); // Create the codedom transform CodeTransform transform = new CodeTransform(LanguageType, bag); MarshalTransform marshalUtil = new MarshalTransform(LanguageType, bag, TransformKindFlags); CodeTypeDeclarationCollection col = new CodeTypeDeclarationCollection(); // Only output the constants if there are actually any List <NativeConstant> list = new List <NativeConstant>(bag.FindResolvedConstants()); if (list.Count > 0) { CodeTypeDeclaration constCtd = transform.GenerateConstants(list); if (constCtd.Members.Count > 0) { col.Add(constCtd); } } foreach (NativeDefinedType definedNt in bag.FindResolvedDefinedTypes()) { CodeTypeDeclaration ctd = transform.GenerateDeclaration(definedNt); marshalUtil.Process(ctd); col.Add(ctd); } List <NativeProcedure> procList = new List <NativeProcedure>(bag.FindResolvedProcedures()); if (procList.Count > 0) { CodeTypeDeclaration procType = transform.GenerateProcedures(procList); marshalUtil.Process(procType); col.Add(procType); } // Add the helper types that we need AddHelperTypes(col); // Next step is to run the pretty lister on it CodeDomPrettyList prettyLister = new CodeDomPrettyList(bag); prettyLister.PerformRename(col); return(col); }
private CodeTypeDeclaration FindTypeEndingWith(CodeTypeDeclarationCollection collection, string endsWith) { for (int i = 0; i < collection.Count; i++) { if (collection[i].Name.EndsWith(endsWith)) { return(collection[i]); } } return(null); }
internal SyntaxTree Generate(NativeSymbolBag bag, ErrorProvider ep) { // Make sure than all of the referenced NativeDefinedType instances are in the correct // portion of the bag ChaseReferencedDefinedTypes(bag); // First step is to resolve the symbols bag.TryResolveSymbolsAndValues(ep); // Create the codedom transform var transform = new CodeTransform(_langaugeType, bag); var marshalUtil = new MarshalTransform(_langaugeType, bag, TransformKindFlags.All); var col = new CodeTypeDeclarationCollection(); // Only output the constants if there are actually any var constList = bag.FindResolvedConstants().ToList(); if (constList.Count > 0) { var constCtd = transform.GenerateConstants(constList); if (constCtd.Members.Count > 0) { col.Add(constCtd); } } foreach (var definedNt in bag.FindResolvedDefinedTypes()) { var ctd = transform.GenerateDeclaration(definedNt); marshalUtil.Process(ctd); col.Add(ctd); } var procList = bag.FindResolvedProcedures().ToList(); if (procList.Count > 0) { var procType = transform.GenerateProcedures(procList); marshalUtil.Process(procType); col.Add(procType); } // Add the helper types that we need AddHelperTypes(col); // Next step is to run the pretty lister on it var prettyLister = new CodeDomPrettyList(bag); prettyLister.PerformRename(col); var code = BasicConverter.ConvertCodeDomToPInvokeCodeImpl(_langaugeType, col, ep); return(CSharpSyntaxTree.ParseText(code)); }
public CodeTypeDeclarationCollection(CodeTypeDeclarationCollection value) {}
public void AddRange(CodeTypeDeclarationCollection value) {}