Is it possible to create a Visual Studio snippet which adds code explicitly in a class constructor? - visual-studio-2010

I want to set up a code snippet (in this case in C#) to create a property which is additionally initialized in the class constructor, which includes that code is not only inserted in the class body but also explicitly in the constructor. Is that possible with just one snipped, and if yes: how?
Thanks in advance!

I'm not sure i've completely understood your question, but you can use this tool :
http://snippeteditor.codeplex.com/
You can write a snippet for a class which contains one property set in the class constructor
Take a look at the class and prop or propfull snippets, it should help you

Related

Get Class of Map in FreeMarker

I want to get a variable's class type in freemarker, used var.class.simpleName;
but if var is a Map, freemarker will process class as a key to find value in var.
it throw exception. how can I do this ? thanks for any suggestion.
First I have to ask why do you need that, because FreeMarker templates aren't supposed to know even if var is Map at all. Maybe your data-model is not what the template needs.
Anyway, for now, I would write a custom TemplateMethodModelEx for this purpose, something that you can use like ${classOf(var)}. Inside the TemplateMethodModelEx implementation you will receive a TemplateModel as the argument value, and then you can check if it's an AdapterTemplateModel, and if so you can get back the original object and get its class. (If it's not a AdapterTemplateModel, then it perhaps isn't even a wrapped Java object, so it doesn't make sense to ask what the class of the original object is.) However, the DefaultObjectWrapper with incompatibleImprovements set to less than 2.3.22 doesn't give AdapterTemplateModel to wrapped Map-s... so in 2.3.21 you will still have to use BeansWrapper, but you can at least set simpleMapWrapper to true.
In 2.3.22 it will be actually possible to write ${var?api.class}... you might use the nightly build. Though it only supposed to solve the problem where you can't access business methods because the primary type of the business class is Map.

How to modify the code of an Abstract class in Magento?

In Magento, I want to modify the code of an Abstract class. Specifically, I want to modify the behavior of the function _getReadAdapter() of the class Mage_Core_Model_Resource_Db_Abstract.
According to this article by Alan Storm, http://alanstorm.com/magento_upgrade_rewrite_override, there are three ways to do it. Modifying the Magento's source code is very bad and not encouraged. On the other hand, it seems to me that I cannot use Magento's override/rewrite system, since the abstract class is neither a model, helper nor block. Is that correct?
As such, the only way to go about this is to make a copy of this particular source code at app/local/Mage, and modify the code of this new copy. Is that right?
Yep, using the codepool app/code/local is the way to go.
This is because abstract classes are not handled by the Magento factories, but literally extended in the class definitions.
Actually you should not modify abstract class, instead, you should modify the derive class that extends this abstract class

Modify Getter and Setter with Database First Approach

I'm currently learning ASP.Net MVC 3 with Entity Framework and want to know if there's a way to modify getter and setter for the model class generated by using the database first approach. Say, I want to sanitize HTML in the model's getter and setter to make sure there's no invalid code get saved in database. What's the best way to do that?
Thanks before.
You will want to use your custom validation routine. The class will be populated automatically, its up to you to determine then if its valid or not.
Use IValidateableObject or override the ValidateEntity method to handle your own validatations.
See Julie's article at:
http://msdn.microsoft.com/en-us/data/gg193959.aspx
In the edmx diagram you can edit the properties of a, well, property, one of which is the access modifier:

Visual Studio 2008 - Is there a way to set a breakpoint for when a class is accessed?

I don't really care which line in a class was hit. I just want to know when the class is accessed.
If it's an instantiable class, put a break point in the constructor. If it has static methods or properties, you would have to put a breakpoint in the first line of each method/property. As far as I know, that's the only way to break when a class is accessed.
Thats not realy how it works.You dont access file(unless your app is reading from it X-P).
You access a class and its methods/properties/contructors. These can be in different files all together (using the partial keyword), so that will make things difficult already.
What exactly are you trying to achieve? If you explain a little more, maybe then we can give a better answer.
In addition to just putting a breakpoint in all instance constructors you could also create a static constructor and put a breakpoint in it if the class is static or if it has static methods. A static constructor is guaranteed to be executed before any static method in a class.

Name of class from its object

How to extract a name of the class from its object?
For example I have a #list object which I know is surely an instance of List class. But how do I extract that directly in code?
This kind of information is rather basic Ruby programming. The answer is:
object.class
Extra tip for the next time: try finding this information yourself in the core library documentation. You know you have some kind of object, just start reading the documentation and you will find some method that suites your needs. Information about the methods you can perform on an object can be found here.
If you want to test for an instance of a specific class, I'd go with something like:
#list.is_a?(List)
Like Edwin said, object.class will give you the corresponding Class object. If you just want the name of the class, use object.class.name.

Resources