Come faccio a creare una classe che eredita da un altro e passa un parametro di tipo in CodeDom?
-
20-09-2019 - |
Domanda
Ecco quello che voglio la dichiarazione della classe risultante a guardare come:
public sealed partial class Refund : DataObjectBase<Refund>
{
}
}
Questo codice (snipped):
targetClass = new CodeTypeDeclaration(className);
targetClass.IsClass = true;
targetClass.TypeAttributes =
TypeAttributes.Public | TypeAttributes.Sealed;
targetClass.IsPartial = true; //partial so that genn'ed code can be safely modified
targetClass.TypeParameters.Add(new CodeTypeParameter{ Name=className});
targetClass.BaseTypes.Add(new CodeTypeReference { BaseType = "DataObjectBase", Options = CodeTypeReferenceOptions.GenericTypeParameter });
Produce questa dichiarazione di classe:
public sealed partial class Refund<Refund> : DataObjectBase
{
}
Che cosa sto facendo di sbagliato?
Soluzione
Credo che la seguente stringa per il BaseType dovrebbe fare il trucco (non testata):
"DataObjectBase`1[[Refund]]"
E 'possibile che è necessario fornire un nome completo per Refund
, almeno compreso il nome assembly:
"DataObjectBase`1[[Refund, RefundAssembly]]"
E si dovrebbe quindi rimuovere la targetClass.TypeParameters.Add(...)
riga.
Altri suggerimenti
Se si utilizza espressioni per CodeDOM potrebbe essere
var cls = Define.Class("Refund",
TypeAttributes.Public | TypeAttributes.Sealed, true)
.Inherits(CodeDom.TypeRef("DataObjectBase","Refund"))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow