in powershell/llcsharp/enums/enum.ts [81:183]
constructor(schemaWithFeatures: EnumImplementation, state: State, objectInitializer?: DeepPartial<EnumClass>) {
if (!schemaWithFeatures.schema.language.csharp?.enum) {
throw new Error(`ENUM AINT XMSENUM: ${schemaWithFeatures.schema.language.csharp?.name}`);
}
super(state.project.supportNamespace, schemaWithFeatures.schema.language.csharp?.enum.name, undefined, {
interfaces: [new Interface(new Namespace('System'), 'IEquatable', {
genericParameters: [`${schemaWithFeatures.schema.language.csharp?.enum.name}`]
})],
});
this.description = schemaWithFeatures.schema.language.csharp?.description;
this.implementation = schemaWithFeatures;
this.partial = true;
this.apply(objectInitializer);
// add known enum values
for (const evalue of schemaWithFeatures.schema.language.csharp?.enum.values || []) {
this.addField(new Field(evalue.name, this, { initialValue: new StringExpression(evalue.value), static: Modifier.Static, description: evalue.description }));
}
// add backingField
const backingField = this.add(new Property('_value', dotnet.String, {
getAccess: Access.Private,
setAccess: Access.Private,
description: `the value for an instance of the <see cref="${this.name}" /> Enum.`
}));
// add private constructor
const p = new Parameter('underlyingValue', dotnet.String, { description: 'the value to create an instance for.' });
const ctor = this.addMethod(new Constructor(this, {
access: Access.Private,
parameters: [p],
description: `Creates an instance of the <see cref="${this.name}"/> Enum class.`
})).add(`this.${backingField.value} = ${p.value};`);
// add toString Method
this.addMethod(new Method('ToString', dotnet.String, {
override: Modifier.Override,
description: `Returns string representation for ${this.name}`,
returnsDescription: 'A string for this value.'
})).add(`return this.${backingField.value};`);
// add Equals Method(thistype)
this.addMethod(new Method('Equals', dotnet.Bool, {
description: `Compares values of enum type ${this.name}`,
parameters: [new Parameter('e', this, { description: 'the value to compare against this instance.' })],
returnsDescription: '<c>true</c> if the two instances are equal to the same value'
})).add(`return ${backingField.value}.Equals(e.${backingField.value});`);
// add Equals Method(object)
this.addMethod(new Method('Equals', dotnet.Bool, {
override: Modifier.Override,
description: `Compares values of enum type ${this.name} (override for Object)`,
parameters: [new Parameter('obj', dotnet.Object, { description: 'the value to compare against this instance.' })],
returnsDescription: '<c>true</c> if the two instances are equal to the same value'
})).add(`return obj is ${this.name} && Equals((${this.name})obj);`);
// add implicit operator(string)
this.addMethod(new Operator(`implicit operator ${this.name}`, {
static: Modifier.Static,
description: `Implicit operator to convert string to ${this.name}`,
parameters: [new Parameter('value', dotnet.String, { description: `the value to convert to an instance of <see cref="${this.name}" />.` })]
})).add(`return new ${this.name}(value);`);
// add static creation
this.addMethod(new Method('CreateFrom', dotnet.Object, {
static: Modifier.Static,
access: Access.Internal,
description: `Conversion from arbitrary object to ${this.name}`,
parameters: [new Parameter('value', dotnet.Object, { description: `the value to convert to an instance of <see cref="${this.name}" />.` })]
})).add(`return new ${this.name}(global::System.Convert.ToString(value));`);
// add implicit operator(thistype)
this.addMethod(new Operator('implicit operator string', {
static: Modifier.Static,
description: `Implicit operator to convert ${this.name} to string`,
parameters: [new Parameter('e', this, { description: `the value to convert to an instance of <see cref="${this.name}" />.` })]
})).add(`return e.${backingField.value};`);
// add operator ==
this.addMethod(new Method('operator ==', dotnet.Bool, {
static: Modifier.Static,
description: `Overriding == operator for enum ${this.name}`,
parameters: [new Parameter('e1', this, { description: 'the value to compare against <paramref name="e2" />' }), new Parameter('e2', this, { description: 'the value to compare against <paramref name="e1" />' })],
returnsDescription: '<c>true</c> if the two instances are equal to the same value'
})).add('return e2.Equals(e1);');
// add opeator !=
this.addMethod(new Method('operator !=', dotnet.Bool, {
static: Modifier.Static,
description: `Overriding != operator for enum ${this.name}`,
parameters: [new Parameter('e1', this, { description: 'the value to compare against <paramref name="e2" />' }), new Parameter('e2', this, { description: 'the value to compare against <paramref name="e1" />' })],
returnsDescription: '<c>true</c> if the two instances are not equal to the same value'
})).add('return !e2.Equals(e1);');
// add getHashCode
this.addMethod(new Method('GetHashCode', dotnet.Int, {
override: Modifier.Override,
description: `Returns hashCode for enum ${this.name}`,
returnsDescription: 'The hashCode of the value'
})).add(`return this.${backingField.value}.GetHashCode();`);
}