How to know such classes like XmlWriter type and its sytnax? - xmlwriter

Might seems silly question and can learn from google but I am new to C# and trying to figure out possible future clarifications. So here is best places to ask.
For example:
Why do not we write;
XmlWriter writer = new XmlWriter("C:\\1.xml",settings);
instead writing;
XmlWriter writer = XmlWriter.Create("C:\\1.xml",settings);
(No new keyword and .Create method.)
What is XmlWriter fully named as a class?
Also how do I know in BCL which class to instantiate how? Like how do I know its syntax if I do not know. What is the ieasy way? How can intelligence help me?

Because this is a factory method, it creates one of the derived classes like XmlWellFormedWriter, XmlAsyncCheckWriter, etc. which depend on settings.

Related

Realm Query - RealmResults<SuperclassType>

Quite new to Realm, but off the bat I like it.
With that said, since progging in Java, I'm using inheritance/polymorphism extensively.
Does anyone know if Realm supports querying for saved data by using a superclass type that extends realm object?
eg:
final RealmResults result = iRealm.where(SuperclassType.class).findAll();
Thanks Kindly
It is not supported right now. You can follow https://github.com/realm/realm-java/issues/761 for that. Until then you need to use composition over inheritance: https://en.wikipedia.org/wiki/Composition_over_inheritance

How to correct the objection about dymanic Object type by FlexPMD?

I have the code in one of my flex file used as labelFunction in a DataGrid.
When I run the FlexPMD to do the code review, it generates an objection about the dynamic type object used in the following method signature and it suggests to use strongly type object.
public function getFormattedCreatedTime(item:Object, column:DataGridColumn):String {
var value:Date=item[column.dataField];
return dateFormatter.format(value);
}
Does anyone know how to rectify it?
Thanks
You have the answer in your question - just use a strongly type object, or perhaps an interface if item can have various types.
But basically there's nothing wrong with using dynamic type objects as long as you know what you're doing. I'd say just ignore the error.
In this case it would be of course possible to type item to something less generic than Object, but some times you can't, or Object is exactly the right type, in this case you can use //NOPMD comment - it will instruct the PMD validator to skip the definition. Of course the good practice is to also explain the reason you used //NOPMD.

Do I need to dispose/close XDocument.Load(string)?

I can't find any info about whether or not I should dispose/close this object after using it... Here is the msdn link: http://msdn.microsoft.com/en-us/library/bb343181.aspx
No, you don't - it doesn't even implement IDisposable. The XDocument and XElement classes use XmlReader under the covers and handle the disposing of the underlying reader for you.
Easiest way to find out is to look if it implements IDisposable

DLR and reflection

Everywhere I read about the new DLR in .net 4, they say that a good use for it is reflection, and the code snippet always shown is something like
dynamic d = GetSomeObject();
d.DoSomething();
d.SomeMember = 1;
What does GetSomeObject() look like? I can't find anywhere that explains that.
I understand that it can be anything, but in the context of reflection what is it? Is it the assembly? an instance of a type?
The return type of GetSomeObject() will be an instance of some type. For example, here's what it might look like:
public Customer GetSomeObject() {
return new Customer("John", "Doe", 12345);
}
And then the code would say:
dynamic customer = GetSomeObject();
string s = customer.FirstName;
// now the "s" variable would have "John" in it
The GetSomeObject() can return anything. It might return a Customer object or a Product. And it doesn't matter! The idea is that when the variable is declared as being dynamic that when you call a method or a property, as you have shown, the compiler will generate code that uses Reflection to try and call the method or property. If they exist then the calls will succeed. If not then you'll get an error at runtime.
In the general case this example is just simplifying the usage of Reflection by having the compiler generate the code for you.
Having said that, if the Customer or Product object implement IDynamicObject themselves then they can do far more advanced stuff.
What you are describing is the duck-typing aspect of dynamic (there are other facets). The answer is that it could be anything:
a true dynamic object (IDynamicObject)
any regular object, via reflection
A useful example (for reading properties, at least) might be an anonymous type; it could also be a COM object, for example - or (in Silverlight) an object in the html DOM. Or it could be your vendor's Customer object that doesn't implement any common interface, but is remarkably like your own InternalCustomer object. Heck, it could be an IronPyton object.
Well, GetSomeObject() could, for instance, return an instance of type _ComObject. That's one of the primary reasons of having it dynamic, I think.
I think that it's more interesting, as far as dynamic, DLR and reflection concerns, to see what happend in line 2 for instance.
using dynmic you go like this
dynamic d = GetSomeObject();
d.DoSomething();
while with reflection it's a bit more noisy
var d = GetSomeObject();
var mi = d.GetType().GetMethod("DoSomething");
mi.Invoke(d,mi);
As I see it, the first one is more elegant and we are talking about an argument less method, things can go really crazy when you are interoping with COM or APIs with long signature methods. I been there ;)

Flex How to organize embeded resources

How to avoid having embeded metadata everywhere in source code(.as or .mxml)?
I have found two approaches:
1. Embed everything in css. But it is kind of difficult to extract from there:
var soundCSSClassDec:CSSStyleDeclaration = StyleManager.getStyleDeclaration("MySound");
var MySoundClass:Class = (soundCSSClassDec.getStyle("url")) as Class;
var myEmbeddedSound:Sound = new MySoundClass() as Sound;
myEmbeddedSound.play()
Embed in resource files. It is easy to extract - resourceManager.getResource("sounds","mySound"). But I feel something wrong with this approach. As i understand resourceManager was desinged for localization.
Any other ideas?
Thanks, Aleksey
It's not wrong to use ResourceManager: in your case (non-localized app) you only support one language (special case of localization for N languages :).
Another approach is to load these resources over the network from some server. Of course, depending on how your application is bundled/used this approach may be inefficient.
Another option is to define an "AssetLibrary" class with lots of public static const typed to "Class"; then reference those either via binding in MXML or through simple assignment in ActionScript.
public class AssetLibrary {
[Embed(source='/assets/image_link.png')]
public static const IMAGE_LINK : Class;
}
Using ResourceManager is a totally valid approach too. It's actually good practice to externalize that from your application since it will make localizing it later on much easier.

Resources