I want to set [XmlIgnore] attribute on class because I want it to be skipped in deserialization, but I want to keep its children to be deserialized.
is it possible to do so?
Thanks.
It makes no sense to want to deserialise children of an attribute you have not deserialised. There is nowhere for them to go - it's a bit like saying I want to create the properties of a class but not the class itself.
If you really wanted to do this I think you would have to write your own XML parser and then find somewhere to stick them - doesn't make sense to me.
Related
I am writing a design-time level editor.
I would like my levels to be defined in JSON, and loaded/saved using Json serialization (Probably NewtonSoft)
The issue I have is this:
In order to have the objects be serializable, they cannot be Unity Objects (e.g. ScriptableObject) (and, indeed, I don’t need them to be – except
In order to bind custom editor stuff to the object’s properties, the objects need to be unity Objects.
I can’t figure out what to do – and figured people must be doing something similar, but my Google skills are failing me (and I have googled A LOT!
As a really simple example:
I have a class called Level, with a string field called LevelName
I have an EditorWindow which has a reference the current Level instance
If Level is a MonoBehaviour, I can say
_so = new SerializedObject(_level); and I can bind a field in my UI to the LevelName field
That works fine – but now I want to save the data – but when I try to serialise it I get an error:
NotSupportedException: rigidbody property has been deprecated
This, my searching tells me, is simply because you can’t serialize unity Object.
But if I change Level to be a POCO (i.e. not inheriting from anything) then the binding doesn’t work because _level is not a UnityEngine.Object
I feel I may be missing something really obvious, but for the moment I am stuck and would really appreciate any help or advice.
I'm trying to implement a plugin system using MAF. The objects I want to pass currently aren't serializable though, and even though I'm sure I could make them serializable I don't think it would be very performant.
Two questions:
1) In order for MAF to pass the actual references across, does an object simply need to inherit from MarshalByRefObject or is there more to it than that?
2) Could I wrap my class in an object that inherits from MarshalByRefObject to get the reference across?
EDIT: Obviously the problem itself has little to do with MAF, but I just wanted to include some context in case someone could point me in an altogether direction to go.
1) Yes, it just needs to inherit from MarshalByRefObject, but any public types inside the class also need to be serializable or inherit from MarshalByRefObject as well.
2) No, this just pushes the problem back since the class still needs to be serializable or inherit from MarshalByRefObject.
The documentation says this is allowed:
ClassMethod GetContacts() As %ListOfObjects(ELEMENTTYPE="ContactDB.Contact")
[WebMethod]
I want to do this:
Property Permissions As %ListOfObjects(ELEMENTTYPE="MyPackage.MyClass");
I get an error:
ERROR #5480: Property parameter not declared:
MyPackage.Myclass:ELEMENTTYPE
So, do I really have to create a new class and set the ELEMENTTYPE parameter in it for each list I need?
Correct syntax for %ListOfObjects in properties is this one
Property Permissions As list of MyPackage.MyClass;
Yes, a property does sometimes work differently than a method when it comes to types. That is an issue here, in that you can set a class parameter of the return value of a method declaration in a straightforward way, but that doesn't always work for class parameters on the class of a property.
I don't think the way it does work is documented completely, but here are some of my observations:
You can put in class parameters on a property if the type of the property is a data-type (which are often treated differently than objects).
If you look at the %XML.Adaptor class it has the keyword assignment statement
PropertyClass = %XML.PropertyParameters
This appears to add its parameters to all the properties of the class that declares it as its PropertyClass. This appears to be an example of Intersystems wanting to implement something (an XML adaptor) and realizing the implementation of objects didn't provide it cleanly, so they hacked something new into the class compiler. I can't really find much documentation so it isn't clear if its considered a usable API or an implementation detail subject to breakage.
You might be able to hack something this way - I've never tried anything similar.
A possibly simpler work around might be to initialize the Permissions property in %OnNew and %OnOpen. You will probably want a zero element array at that point anyway, rather than a null.
If you look at the implementation of %ListOfObjects you can see that the class parameter which you are trying to set simply provides a default value for the ElementType property. So after you create an instance of %ListOfObjects you could just set it's ElementType property to the proper element type.
This is a bit annoying, because you have to remember to do it every time by hand, and you might forget. Or a maintainer down the road might not now to do it.
You might hope to maybe make it a little less annoying by creating a generator method that initializes all your properties that need it. This would be easy if Intersystems had some decent system of annotating properties with arbitrary values (so you could know what ElementType to use for each property). But they don't, so you would have to do something like roll your own annotations using an XData block or a class method. This probably isn't worth it unless you have more use cases for annotations than just this one, so I would just do it by hand until that happens, if it ever does.
I used InheritedExport attribute, and have a tree of objects. Think IMessageSender where I have SmtpSender, TextSender, HttpPostSender. But through decorator pattern I have additional classes that also inherit this MEF InheritedExport attribute.
However when composing the senders, I do not want to decorators to be composed.
Now Lazy could help, whereas I only retrieve the objects that are not of the Decorator type. But I can't ask Lazy what T actually is. Perhaps metadata could help, but the metadata only applies to the object that has this metadata. I don't want to force implementers (read: developers) to add the correct metadata to their decorator or sender.
Any ideas? Thanks!
I needed to do something similar. In the end I opted to add metadata that gave me the required information, but I can see why that's not ideal for your.
One other possibility would be to switch from using an automatic import via attributes, and explicitly call CompositionService.GetExports instead. Then you'd end up with an IEnumerable of Export objects. This would let you get at the contract name (usually, but not always, the type name) and the contract metadata. I think that the contract metadata always includes the type name, so you'd have the information you're looking for.
You can use the following code, to get the type of a Lazy member:
.GetType().GetProperty("Value").PropertyType
I have a little bit of a curve ball for you. Maybe just a design issue...maybe even something as simple as me not understanding Data annotation providers.
Anyway here we go:
I have a class which represents some model data. Let's say it represents a package/box/carton.
It actually represents all of these things so I use the class in several different views. Sometimes I want the attribute of the field Package_Description to be
So that it shows up as Box Number : input box here.
Now if i want it to appear as "Carton Name" my only option would be to sub type it. Or use a separate class to have the annotations for this class. My quandary is that some of the field names are user configurable and therefore I cannot have a static definition!
(By the way i am using third party librarys [Telerik MVC Grid] do display these field names so i cannot change the fact that it's looking at data annotation )
So I just need to know is there a way to add attributes dynamically?
Create an anonymous type on the fly, sub class the original and then add attributes using reflection?
Or what other options are open to me, do I need to somehow implement a different annotation provider?
Attributes are part of the definition of the type. Because of that, you can't modify attributes of existing classes during runtime.
You could create a new type during runtime (not an anonymous type), but I think that's not such a good idea. I'm sure whatever component you're using, it allows you to specify the appearance explicitly.