How to add "ref" as string name in C# - string

I want to name my string value 'ref' without any capital letters. Since ref is a value i can't do that.
Is there a way around this?

As you've discovered, ref is a keyword and can't be used directly in this manner. The usual advice is "don't use keywords as names". For things like deserializing JSON which has a property called "ref" you can often get around it with mapping your otherwise-named property to that JSON property with configuration or a property attribute, depending on the JSON serialization library you're using.
But if you really do need to use a keyword as a name like this, or if it's just easier than mucking around with things like serialization libraries, the language does provide a way to do that. Simply prefix the name with an #:
public string #ref { get; set; }
This approach is most commonly seen in things like Razor web pages where we need to specify client-side HTML attributes in server-side C# objects, and a common HTML attribute name is class.

Related

JAXB how to remove anything from JDefinedClass

i am using jaxb to generate code from an xsd.
The generated code contains a lot of annotations; for classes and fields.
I am trying to use com.sun.tools.internal.xjc.Plugin to modify the generated code.
In the plugin run() method we are given an Outline class from which we can get ClassOutline. ClassOutline has an JDefinedClass final member which has the info about actual class which will be generated.
If i want to add anything, there are apis in JDefinedClass which can be used. But if i want to remove something, there is no way.
e.g. i cannot clear annotations, because the JDefinedClass.annotations() method returns an UnmodifiableCollection. so i cannot clear it or remove anything from it.
i tried to create another JDefinedClass by invoking the _class method but the ClassOutline.implClass variable is final, so i cannot set it.
how to get a JDefinedClass which does not have any annotations?
is there another phase of code generation which i can trap into to really control the generation of JDefinedClass?
The code model is, indeed mostly "write only". But, speaking of annotations, you have probably missed the methods like com.sun.codemodel.JDefinedClass.removeAnnotation(JAnnotationUse) and com.sun.codemodel.JMethod.removeAnnotation(JAnnotationUse) (implemented from com.sun.codemodel.JAnnotatable.removeAnnotation(JAnnotationUse)).
So they're there. You can remove annotations with the normal CodeModel API.
As I can see, you can also remove fields and methods from classes. So what exactly are you missing?
JDefinedClass.annotations() It return an unmodifiable collection object and you cannot modify them.
So work around for this, you can restrict annotation addition/deletion at class and field level before building JCodeModel.
You need to create a custom Jackson2Annotator class which extends Jackson2Annotator and override their methods according to your requirement.
Following are few methods which are being used for specific type of annotation property:
propertyOrder(OTB JsonPropertyOrder)
propertyInclusion(OTB JsonInclude)
propertyField(can be used for custom defined annotation at field level)
More you can discover by looking Jackson2Annotator class what fit into your need.

How to access certain EStructuralFeatures of an EMF Model?

I know that there are ways to access an EAttribute of an Eclipse EMF model by its featureID or by its name via different indirect approaches. For that I found the following: Eclipse EMF: How to get access EAttribute by name?
But what if I don't know the name of the attribute I want to get? Let's say, based on the design, the model has some fixed attributes by the developer, along with the features that can be set dynamically by the user.
So, for the time being I use the getEAllStructuralFeatures() and use indexes via get() to reach to the by-the-user-created attributes, since I know that the list I get will have the fixed attributes of the model as its first elements beginning with the index 0. But I find this solution unclear and inefficient. Also in some cases, that I want to work, not suitable.
E.g: IEMFEditProperty prop = EMFEditProperties.list(editingDomain, EMFMODELPackage.Literals.EMFMODEL.getEAllStructuralFeatures().get(X));
Do you know a solution or a workaround for this problem? As far as I can see, there are no direct methods to get such dynamically created features of the model.
Every help will be appreciated.
I have been working on a similar case recently where I first tried to define an EStructuralFeature to access exactly the setting/attribute of the object that I needed.
But if you look at how things work internally in ECore, you will find out, that there is no way this will ever work, since the indices are bound to the object identity of the EStructuralFeature objects created at runtime for the specific context (i.e. EClass instance).
My approach was then to either inspect the features proposed by EClass.getEAllStructuralFeatures or to iterate over the features and inspect the object returned by EObject.eGet for this very feature (where EClass eClass = eObject.eClass()).
Example: In a UML profile I have defined a UML Stereotype called "Bean" with a property called FactoryEntity. The property shall reference a UML Class with the Stereotype "Entity" that is closest to this very bean and for which a static factory method will be generated.
In the model I would then have one UML Class typed as Bean and one as Entity.
And for the Class typed as "Bean" I would then set a value for the attribute/property factoryEntity defined in the profile.
The question was then how the property value would be accessible in ECore. I ended up iterating the List of available EStructuralFeature of the EClass of the EObject and checking the type of the object returned by eGet.
final EObject eObject = (EObject) holdingClass.getValue(stereotype, stereoTypePropertyName);
final EList<EStructuralFeature> allEStructFeats = eObject.eClass().getEAllStructuralFeatures();
for(EStructuralFeature esf : allEStructFeats)
{
final Object o = eobject.eGet(esf);
if(o instanceof org.eclipse.uml2.uml.Class)
{
return (org.eclipse.uml2.uml.Class) o;
}
}
Maybe that is not the most elegant way to access structural features but it is the only one I thought was robust enough to last.
Please let me know if you have any suggestions on how to improve this.

Can we get declared properties of a Groovy class by its order?

I created a plain Groovy class (i.e Person class)with some properties. Now I want to get those declared attributes (which I've defined in my class) with their order, but I don't know how to do it.
I've tried to use Person.metaClass.getProperties() but it retrieves not only declared properties but also built-in Groovy ones.
Could you please help me on this: just get declared properties by its order when declaring.
Thank you so much!
I can't see a use case, but the compiler could reorder all fields declaration while creating bytecode. I'm pretty sure ordering is not a constraint on fields though it should mostly be the case for not modified/enhanced class
As per the JVM spec, generated fields should be marked SYNTHETIC (like generated methods) in the bytecode, so you can test with :
Person.getDeclaredFields().grep { !it.synthetic }
and filter the base Groovy fields like ClassInfo,metaClass and others beginning by __timestamp
But I'm not a specialist, there could be another way I don't think of
There was a question about this on the mailing list back in February of this year
The answer is, no. There is no way to get properties in the order they are declared in the class without doing some extra work.
You could parse the source file for the class, and generate an ordered list of property names from that
You could write a custom annotation, and annotate the fields with this annotation ie: #Order(1) String prop
You could make all of the classes where this matters implement an interface which forces them to have a method that returns the names of the properties in order.
Other than that, you probably want to have a re-think :-(

Use of object vs string vs enum

I have a situation that may seem ridiculus but I have not been able to figure out a good enough solution. To simplify things the problem is something like this, suppose you have an object like Manufacturer that has a countryname property (like car.Manufacturer.CountryName) and you want to be sure that the countryname property can not have duplicates or misspellings or other errors.
It is basically a string property but a string can be anything which I do not want. An object seems like overkill and an enum means I have to recompile if new countries are to be added or existing countries to be changed.
I could easily control this in a GUI but I need to control this in the application code. So I have an object with a property that could be a string, an object or an enum (or other) and I cant decide which to use. So my options are something like this:
a) Control this in the GUI and do not check this in the application code, taking the risk that I can get "illegal" country names.
b) Make an object (Country) and use that, which is overkill and makes the code more complex, but I have complete control over duplicates and all that stuff.
c) Use an enum and hope that I do not have to recompile too often. It is simple and effective but a static solution.
d) Use an internal string list of valid country names and have CountryName as a string property and make sure that it is validated against that string. I get validation and CountryName is just a simple string, but what if I change that internal string of valid country names? Than I have to make code the revalidates all Manufacturer objects in the program too make sure they still have valid contry names.
I am not totally sure how important it is too have valid country names but the more I think about this the more I realize that I am in a grey zone. An object, or struct, is too much, an enum too static, a string too simple.
I could be totally overcomplicating things here but I would really like too know what to do, or rather how to think, when you get into this grey zone of object vs string vs enum.
Thankfully yours!
Hal
c) Use an enum and hope that I do not have to recompile too often. It is simple and effective but a static solution.
From this statement I can infer that your data can be subject to change. In these situations, I suggest using an external data store (a database, text file, XML file, whatever).
If country name data integrity is important to you I'd store a list of valid country names in a database or other persistence method, query and cache them inside the application.
Then you can add one post-deployment without recompiling or changing code and with caching you don't incur a lot of overhead for the extra database query.
Go with the object.
It really comes down to a case of do the 'complex' work now and life is easier later on or take the easy route now and life may be more 'complex' later.
Also considering you are actually worried about all the negatives of options b, c & d then just remove your worries!
Also as others have suggested your data should be stored externally to the code (database, xml file etc...) writing an object to wrap this would be your best solution.
I would use a dictionary stored as a separate file, like a XML property list, something like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="1.0">
<dict>
<key>UK</key>
<string>United Kingdom</string>
<key>FR</key>
<string>France</string>
<key>SP</key>
<string>Spain</string>
</dict>
</plist>
There might be another option:
use a list of Country names stored in an external (possibly human-readable) file. When your program starts, load the country names in an array of strings (or in some other ordered container), and use the order in the list to assign a number to each country (if you use an array, just use the index). A variable containing a country is now just an integer.
Another trick is to use a separate object (which has only one instance) to handle country names. This will ensure that all objects are using the most recent version of the list of country names. Manufacturer objects then do not have a string list, but a reference to the unique object containing the string list.

How to add a display name for a decorator in Visual Studio DSL (Domain Specific Language) Tools?

In my DSL project I have a shape with a number of decorators that are linked to properties on my domain class. But even though ieach decorator has a DisplayName property (set to a meaningfull value) it does not appear in the generated DSL project. (I have not forgtten to use regenerate the t4 files.)
Do I have to create another decorator for each property that only has the display name as a value that I wish to display or is there some other way that I can't figure out right now?
I assume by a display name for the decorator you mean you want the element in the generated DSL to appear as "Example = a_value" where a_value is the actual value and Example is the property name.
What I've done with this in the past is to create second property "ExampleDisplay" that's not browsable and is what the decorator actually points to. I then set the Kind property of the ExampleDisplay to "Calculated". You then need to provide the method that the toolkit tries to call to display the decorator which you can do a partial class.
partial class ExampleElement
{
string GetExampleDisplayValue()
{
return "Example : " + this.Example;
}
}
This is not ideal as you don't get a good way of setting the property on the DSL diagram you have to use the properties window. (There's sometime lags from the property window unless you hook into the update of the underlying property too). Getting the slick editing in the GUI that actual DSL toolkit does maybe possible but I haven't found out how.
It maybe worth ask VSX forums if you haven't already done so.

Resources