Is there a way that I can add a default using directive to ScriptCS? So when I start ScriptCS I do not need to include the namespace for classes in that namespace.
For example, I may need to interact with the Process class within the System.Diagnostics namespace a lot. If I want to create a new instance of this class, I need to do
var proc = new System.Diagnostics.Process();
I would rather do
var proc = new Process();
I know that I can add the using statement each time I start ScriptCS, but I would like it to always be there.
You can add additional assembly references and using statements into your en and by writing a Script Pack. You can see an example of injecting using statements in the Web API example.
A script pack can do this as Justin mentioned. An alternative option would be to use global modules, example here (https://github.com/scriptcs-contrib/scriptcs-sample-module) however currently they don't support this nicely. If the module had a hook for getting all the services after they were created, you could do this.
I filed this bug (https://github.com/scriptcs/scriptcs/issues/472) to allow addressing this in a much cleaner fashion.
Looks like this can be accomplished by modifying the DefaultNamespaces property of the ScriptExecutor class to include the namespace you want to always have.
You would need to download the source (or fork a new branch in github), modify and compile yourself. You would then put the new ScriptCs.Core assembly in the install folder (C:\Users\USER_NAME\AppData\Roaming\scriptcs). This would replace the existing assembly. If you do this you will lose any changes if you are updating through chocolatey
Related
Currently :
I have the following Using in my program.
using Mtx;
which allows me to use Mtx.* properties. It refers to a file in "Externals" folder.
Path is : Externals/Mtx.dll
Needed :
However, for debugging purposes, I'd like to now have the whole mtx solution in external and use it.
Path is : Externals/Mtx/(solution in there with all folders)
How can I do so, so instead of Using refers to the Mtx.dll, it now refers to the solution and build it has part of its own?
I think that you are misunderstanding some concepts and mixing things. Let me explain with your own explanation:
I have the following Using in my program.
using Mtx; which allows me to use Mtx.* properties. It refers to a
file in "Externals" folder.
Path is : Externals/Mtx.dll
The using keyword allows you to use the classes inside a namespace without typing the whole namespace everytime. It has nothing to do with dll classes, you can use all the public dlls insidea class just using the whole namespace + the class name only adding it as a project reference.
Needed :
However, for debugging purposes, I'd like to now have the whole mtx
solution in external and use it
For debugging purposes all you need are the pdb's of the dlls used and you will be able to debug any class considering you have its source code without changing anything else.
I need to declare an attribute for coverage exclusion in my code, the issue is that i have a project group and i wish to create it somewhere where i can access it from all projects when i need it, right now i have it outside of the namespaces so it would be easier to use, and its declared in each project like:
public class CoverageExcludeAttribute : Attribute
{
}
is there any better way to achieve this goal in a way it could be access anywhere in my project group and declared only once, without having to add its namespace (e.g by using the global namespace) to each file i use the attribute in?
Thank you
While I actually agree with P.Brian.Mackey, I think the only way to do it is exactly as DjKraze said:
Create a new micro-project of type ClassLibrary, add a single .cs file with your Coverage(..) class and ensure that class is inside no namespaces block. Then build it and for each one of the other projects do a Add-Reference to that micro-project you just created.. That way it will surely work, and you will have a handy place to put any further 'common code' to be available everywhere.
However, each project will have to be updated with the reference. This is the minimum requirement - all in all, if you want to use anything instead of copying, it must be referred..
Sorry, almost no other options for such thing!
The other way is to .. ugh, copy. You can easily set up a simple pre-build script that will copy given .cs file to each one of your projects, but "adding" the file to the .csproj's build list is a bit harder, still possible with use of some Ruby or Python or friends...
Hm.. saying that, It may be possible to write a pre-build script to inject a reference to the micro-project automatically.. But I wont know if this is worth doing. Do you have more than 50-100 projects? Else, probably it's not worth..
This only applies to VS2010 and above
If you want some source code defined in each of your projects, but without a project reference, take a look at some of the functionality provided by NuGet, especially Source Code Transformations. These allow the addition of some source code to the project when you add the NuGet package to the project.
You can use Dependency Injection
http://en.wikipedia.org/wiki/Dependency_injection
The most popular are: Microsoft Unity, Ninject, NHibernate, StructureMap, Autofac.
Good luck!
Is it possible to add a using declaration to a namespace globally?
For example, I want all of my classes under the "MyApp.Models" namespace to include the namespace System.Linq.Dynamic.
In asp.net you can do this for view by adding the namespace in the web.config. Is there a way to do this for class files as well?
No. It is not possible to add a global using declaration to a C# project. You must specify the complete set of using in each .cs file
It's not possible to do what you want with obvious functionality but you could create a template that you generate your classes from and do that by namespace, the templates could include the using and this would give you the desired effect.
You could do this with basic T4 templates or something like resharper also has this functionality.
However it is a bit pointless, lots of productivity tools will bring in the using statements you need given code you write, resharper and others do this, so it is not neccessary to have a template that does this as you may end up in each namespace not using the declaration then find yourself having to delete it !
Theres not really an issue with having using statements that are unused by the way its just messy.
Ooh just in case you aren't sure what t4 templating is here is a quick link
I don't believe this is possible out of the box, but you could certainly automate with a Visual Studio plugin or a plugin for a tool like Code Rush.
I am using Structure Map to load plugins from a child directory.
Both the main app and the plugin reference the FileHelpers dll. FileHelpers has attributes that you put on a class to define what the record is delimited by. These are defined in my plugin. eg.
[Delimited('\t')]
public class Test {
public string name;
}
The FileHelpers utitlity is run from the main app using the class definitions provided by the plugins. If I put the plugin dll in a directory underneath the main application then I get an issue with the FileHelpers library complaining that the attribute cannot be found, however if it is placed next to the main library (same folder), then it works fine.
I have placed some further debug statements into my code and have found that if
var type = typeof(Test);
var attributes = type.GetCustomAttributes(true);
is used and not the specific (the one FileHelpers is using)
var attributes = type.GetCustomAttributes(typeof(DelimitedAttribute), true);
then it finds the custom attributes without any troubles.
I thought this may have been a SM thing but have tried MEF and by doing it using Assembly.Load() and the same thing happens.
I think you are running into the issue described here.
According to the blog post linked in the answer, it looks like the plugin dll would need to be strongly named and fully trusted, otherwise GetCustomAttributes will filter out the DelimitedAttribute. You could try to add the AllowPartiallyTrustedCallers attribute to the plugin assembly.
What is the equivalent command to PHP's include() in C# ?
For example, PHP's include is used as so : include("ex.php");
Can I do the same in C#?
If you mean in ASP.Net using C# you can create a user control (.ascx) and add it in your .aspx page.
If you are doing MVC you can create a partial view.
The closest thing I can think of would be after creating an ascx user control named "MyUserControl"
in your page_load or pre_render :
MyUserControl cont = new MyUserControl();
this.Controls.Add(cont);
There is no such thing in C#. It's not a scripting language, so including a block of script wouldn't make sense.
What are you trying to accomplish? There are ways to do similar things in C#.
There is no direct equivalent. You use references to "link" with other CLR assemblies (access their type information), and the using directive to import namespaces.
For example, the FontCollection class is in the System.Drawing.dll assembly, and the System.Drawing.Text namespace. So you would add System.Drawing as a reference, and add the line:
using System.Drawing.Text;
I'm not sure, if this is what you want to do. But just for the case, maybe you have a look at:
<%
Response.WriteFile( "YourFile.whatever" )
%>
In addition to previous answers mentioning the using Directive and adding references to assemblies to your project (or at command line when compiling) there is a way to load other compiled .NET assemblies at runtime.
Assembly.Load will load an assembly (compiled c# file/.dll) into memory, allowing you to find and use types within that assembly. This can be used when building a plugin architecture. You publish an assembly with an interface for a plugin contract. Plugin makers can link to that that assembly and implement your interface. Your application can then load plugin assemblies, check for any types implementing your plugin interface and load and use those types into your application.
The only thing comparable in C# is using, which imports namespaces defined in assemblies referenced from the project. You cannot "include" a file in the sense that you dump the content right into your code.
For example, if your project references the System.Xml assembly, then the following code would allow you to access all of the classes in that namespace without fully qualifying their names:
using System.Xml;
This will let you use the type System.Xml.XmlDocument, for example, by specifying it as XmlDocument instead of its full type name System.Xml.XmlDocument.
There is no such thing in C#.
You're going to want to create an instance of a C# class and use that to invoke methods/attributes from other 'packages' (C# classes).
You can also use a using direction to be able to references assemblies from other projects.
Using is vaguely similar. It references another class that can then be used from that file, but it doesn't include the contents of that file directly inline.
using system;
(right at the beginning of a file)
Use this for c#
#RenderPage("header.cshtml")
This is taken from here:
http://www.w3schools.com/aspnet/showfile_c.asp?filename=try_webpages_cs_002
Although I know this post is old, but people stubming to this post can refer to it.
Well, here is what I did ... not sure if this is the right way, but it works...
In the .ascx File, specify a div as container to received the contents of included file.
<div id="containerForSomeMarkup" runat="server"></div>
In the .ascx.cs file, initialise this in the onInit() method or other methods as required...
containerForSomeMarkup.InnerHtml = File.ReadAllText("Full path of file to be included");
Coming from PHP world, for me, this approach helps keep the markup organized and intelligble...
The using keyword is what you're looking for.