CSScript: Inject System.linq inside "CreateFunc" - c#

This code:
func = CSScript.CreateFunc<int>(#"int f(int[] inputs) { return inputs.Max(); }");
int max = func(new int[]{ 235,123,675,111 });
is throwing:
error CS1061: 'System.Array' does not contain a definition for 'Max' and no extension method 'Max' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
I need to inject System.Linq inside my function "func"
....How can I do that?

This is from the CS-Script Documentation:
Hints And Tips
When you need to reference many assemblies (and particularly when these assemblies are commonly used) it is convenient to combine all //css_reference statements into single file containing no code and include this file into your primary script.
The following code includes linq.includes.cs file containing references to all assemblies required for programming against LINQ:
//css_include linq.includes.cs;
using System;
class Test
{
static public void Main( string [] args)
{
....
This is the content of the linq.includes.cs file:
//css_ref System.Core;
//css_ref System.Data.ComponentModel;
//css_ref System.Data.DataSetExtensions;
//css_ref System.Xml;
//css_ref System.Xml.Linq;

Related

How to resolve the class name conflict problem in C#

I use a third party dll which does not use namespace, it contains a enum named Speaker.
// Decompiled with JetBrains decompiler
// Type: Speaker
// Assembly: StreamSDK, Version=1.0.6782.19546, Culture=neutral,PublicKeyToken=null
// MVID: 82353EB3-505A-4A47-8EEB-ED74ED4FC9B9
// Assembly location: /Users/haha/test/Assets/_ThirdParty/SteamSDK/Core/XMLSerializer/StreamSDK.dll
public enum Speaker
{
remote,
local,
none,
}
My local project also has this class name under a specified namespace Photon.Voice.Unity;. After I import the dll, error happens because the compiler treats the local Speaker as the third party's Speaker.
I already use namespace in my local project:
using Photon.Voice.Unity;
Error happens in the following codes :
private void OnSpeakerCreated(Speaker speaker)
{
speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}
The error:
error CS1061: 'Speaker' does not contain a definition for 'gameObject' and no accessible extension method 'gameObject' accepting a first argument of type 'Speaker' could be found (are you missing a using directive or an assembly reference?)
After I add the full namespace, the codes are passed.
private void OnSpeakerCreated(Photon.Voice.Unity.Speaker speaker)
{
speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}
But I don't want to do that I just want to ban the use of the third party Speaker in the specified cs files or any other ways that I don't need to change the current codes.
If you are just lazy to write Photon.Voice.Unity.Speaker every time, you can create an alias for using a using alias directive:
using PhotonSpeaker = Photon.Voice.Unity.Speaker;
Now you can write:
private void OnSpeakerCreated(PhotonSpeaker speaker)
{
speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}

C# Missing Reference

When running this code:
using System;
using System.Text.RegularExpressions;
namespace WebAutomationDemo
{
public class ExtractDigitsFromString
{
public static void Main(string[] args)
{
string inputData = "No 38, Bunder Garden Street, Perambur, Chennai - 600011";
var data = Regex.Match(inputData, "/s([a - z][A - Z]) +/s -/s/d[6]").Value;
Console.WriteLine(data);
Console.ReadLine();
}
}
}
I get the following error:
The type or namespace name 'RegularExpressions' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
This happens even after referencing System.Text.RegularExpressions assembly.
Are you still facing the same error,may be your assembly System is missing RegularExpressions method as stated by many.
Goto Object browser i.e click on any project solutions's references(expand) open any assembly will take it to take object browser window.
Now check whether the System.Text contains the RegualrExpressions method.
Object browser
If you follow the link there is an image of my object browser that do contains System.Text.RegularExpressions assembly.
If its missing ,download a valid assembly from MSDN
Below is one of link for a System.dll,download it or google it you can downlaod it easily.
https://www.dllme.com/dll/files/system_dll.html
Then goto references of your project and then add the downloaded assembly.

Namespace not found error when compiling assembly from files

What I'm trying to achieve is generate a project dynamically from c# classes generated by me.
This classes' content are a similar content of code-first code generation of entity framework.The content looks as follows:
namespace ElasticTables
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.KeyAttribute;
[Table("address")]
public partial class address
{
[Key]
public decimal id { get; set; }
public string name { get; set; }
}
}
I generate this files from the tables in my database, and then try to compile it programmatically so that I can reference the generated project in another project that works with an API.
The main errors while compiling is:
The type or namespace name 'KeyAttribute' does not exist in the namespace 'System.ComponentModel.DataAnnotations' (are you missing an assembly reference?)
The type or namespace 'Key' could not be found
The type or namespace 'Table' could not be found.
I'm using 'CSharpCodeProvider'
var provider = new CSharpCodeProvider();
var options = new CompilerParameters
{
OutputAssembly = "ElasticTables.dll",
CompilerOptions = "/optimize"
};
And I have the following referenced Assemblies
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.dll");
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.SqlServer.dll");
I have an string array with the files' paths called sources, and I try to compile with the following line
CompilerResults results = provider.CompileAssemblyFromFile(options, sources);
Help is much appreciated.
You need to reference all needed assemblies (as the error says), so you need to add, I'd say at the very least:
options.ReferencedAssemblies.Add("System.dll");
options.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");
Others might be needed
About the comment in your question, yes, you should specify options.OutputAssembly
Also, in your generated code:
using System.ComponentModel.DataAnnotations.KeyAttribute;
KeyAttribute is not a namespace, so it'll probably give an error when compiling.
I'd also take the usings before the namespace. This is not strictly needed and not an error, but it's the common practice (and that way you are sure the referenced assemblies are from the global namespace, and not childs of the namespace your class is in [just in case there's a name conflict])
Have you tried to add also a reference to "System.dll" and "System.ComponentModel.DataAnnotations.dll" (for the System.ComponentModel stuff) ?
(as you may indeed be missing an assembly reference)
options.ReferencedAssemblies.Add(
Path.Combine(
Directory.GetCurrentDirectory(),
"System.ComponentModel.DataAnnotations.dll"));

How to use fizzler external lib under Monodevelop

I got a compile error using Fizzler lib (http://code.google.com/p/fizzler/) in Monodevelop IDE under Ubuntu 10.
I added .Net Assembly References and autocompletion works file, but error during the compilation occurred.
Code here:
using System;
using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;
using System.Collections.Generic;
namespace test
{
class MainClass
{
public static void Main (string[] args)
{
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(#"some html");
HtmlAgilityPack.HtmlNode document = html.DocumentNode;
document.QuerySelector("a");
}
}
}
Error CS1061: Type HtmlAgilityPack.HtmlNode' does not contain a definition forQuerySelector' and no extension method QuerySelector' of typeHtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?) (CS1061) (test)
Are your HtmlAgilityPack.HtmlNode has that definition provided? Check again the API documentation. Please the error is clear by itself.
You may have found the answer to this question by now but I will post one anyway, since people may come across this page while looking for an answer.
Replace:
using Fizzler.Systems.HtmlAgilityPack;
with:
using myFizzler= Fizzler.Systems.HtmlAgilityPack.HtmlNodeSelection;
and call it like this in your project:
HtmlNode h2 = myFizzler.QuerySelector(document, "#fbTimelineHeadline h2");
I hope this helps.

missing a using directive or an assembly reference?

I am getting this error Error 4 The type or namespace name 'Sample' could not be found (are you missing a using directive or an assembly reference?) F:\oadmin\sea\Controls\Dlg.cs 11 7 Controls
here i added Samplein reference section,,,then i used it as Using Sample,,i dont know why i am getting this error
this is the class
namespace sample
{
public class Server
{
public class client
{
Bool sent = true
}
}
}
Can i share exe,,this Sample is an exe
Perhaps because your namespace is sample and not Sample?
So if your Dlg namespace is different it needs to have a using sample line not using Sample.
Or perhaps your class is in another project within your solution and you need to include the hierarchy of namespaces?
Your namespace "sample" is lowercase, but you are referring to it in uppercase "Sample" ...
EDIT: You Bool sent is internal, it cannot be accessed from outside of the assembly

Categories

Resources