I am writing a Fody Addin and I am able to inject my code and to provide error messages to the user. I am able to determine the sequence points of instructions, but I cannot find a way to find the sequence points of CustomAttributes.
I need to get this information to provide the debugger a hint where to find the location of an error, in case that an attribute has been applied wrongly.
So basically I have something like this:
[MyAttribute]
public void Test()
{
}
Now I want to get the SequencePoint of the MyAttribute attribute.
**Edit: ** As I got down voted (without any information why) here some additional information. I can access the sequence point of instructions like this:
public static SequencePoint GetSP(MethodDefinition method)
{
return method.Body.Instructions
.Where(instruction => instruction.SequencePoint != null)
.Select(instruction => instruction.SequencePoint)
.FirstOrDefault();
}
That works fine for instructions but when I access an attribute I am not sure how to get the sequence point:
public static SequencePoint GetSP(MethodDefinition method)
{
var attribute = method.CustomAttributes.First();
// what to enter here to get SequencePoint of attribute?
}
this is not possible. attributes dont have sequence points. i suggest you just use the first sequencepoint for the method instead
Related
At this moment I'm writing Roslyn analyzer with fixer that checks if constructor arguments are checked for null. And if not, fixer will add that check.
So, basically for code
public Foo(string param1)
{
}
it should produce
public Foo(string param1)
{
if (param1 == null)
throw new ArumentNullException(nameof(param1));
}
But in some cases code style could be different and it is desired to get "if" with braces like
public Foo(string param1)
{
if (param1 == null)
{
throw new ArumentNullException(nameof(param1));
}
}
or even (not sure if there any settings for that, maybe I'll add separate fixer for it):
public Foo(string param1)
{
this.param1 = param1 ?? throw new ArumentNullException(nameof(param1));
}
So, there are many options for a possible fix and I wonder - is it possible to read code style settings inside of fixer? I've checked
DocumentOptionSet options = context.Document.GetOptionsAsync()
but didn't found how to use it properly and not even sure that it is what I need.
Will appreciate any tips and ideas
UPDATE 1
So, seem that I found how it is supposed to work:
options.GetOption(CodeStyleOptions.QualifyFieldAccess);
And I can see (CSharpCodeStyleOptions.cs) with needed option PreferBraces. However, this class is internal and I can't use it in fixer.
UPDATE 2
I tried Simplifier.ReduceAsync() method, but seem that it doesn't do what I need.
UPDATE 3
So, it seems that we all treat that property wrong, because of its naming.
Here is my issue in Roslyn github. The main idea - "Prefer braces" option should be named "Require braces". New issue for this change.
And it is not supposed to get these settings from analyzer/fixer code.
I'd like to automatically try to convert input parameters from Excel-friendly types to ones that are useful in my AddIn and vice-versa back to Excel with the return values. For example, I'd like to define a an Excel function (as a C# method) like:
public static Vector<double> MyFunction(Vector<double> inputVector)
{
// do some stuff to inputVector
return inputVector
}
I'd like for it to convert my input params and return value 'behind the scenes', i.e. I define some generic conversion method for converting from object to Vector<double> and vice versa, and this is called before they are passed in/out of my defined method.
Is this possible? I found ParameterConversionConfiguration in the github repo but I'm not quite sure how to use it. Are there any examples or further documentation available? I can see that I might need to register my type conversions somehow, but I'm not sure how to proceed.
EDIT: After some more playing around, I did this to convert a return value from a Matrix to an array:
public class ExcellAddIn : IExcelAddIn
{
public void AutoOpen()
{
var conversionConfig = GetParameterConversionConfig();
}
static ParameterConversionConfiguration GetParameterConversionConfig()
{
var paramConversionConfig = new ParameterConversionConfiguration()
.AddReturnConversion((Matrix<double> value) => value.ToArray());
return paramConversionConfig;
}
}
But upon loading the .xll, Excel spits out an 'unsupported signature' error. Am I on the right track? What else do I need to do?
There's a complete sample add-in that uses these Excel-DNA Registration extensions here: https://github.com/Excel-DNA/Registration/tree/master/Source/Samples/Registration.Sample
Some details relevant to your question:
You actually need to get the function registrations, apply your conversion and the perform the registration in your AutoOpen:
public void AutoOpen()
{
var conversionConfig = GetParameterConversionConfig();
ExcelRegistration.GetExcelFunctions()
.ProcessParameterConversions(conversionConfig)
.RegisterFunctions();
}
You might want to suppress the default processing by adding an ExplicitRegistration='true' attribute in your .dna file:
<DnaLibrary Name="My Add-In" RuntimeVersion="v4.0" >
<ExternalLibrary Path="XXX.dll" ExplicitRegistration="true" .... />
</DnaLibrary>
I'm using AngleSharp to parse HTML5 at the moment what I'm doing is wrapping the elements I want to parse with a little bit of HTML to make it a valid HTML5 and then use the parser on that, is there a better of doing it? meaning, parsing specific elements directly and validate that the structure is indeed HTML5?
Hm, a little example would be nice. But AngleSharp does support fragment parsing, which sounds like the thing you want. In general fragment parsing is also applied when you set properties like InnerHtml, which transform strings to DOM nodes.
You can use the ParseFragment method of the HtmlParser class to get a list of nodes contained in the given source code. An example:
using AngleSharp.Parser.Html;
// ...
var source = "<div><span class=emphasized>Works!</span></div>";
var parser = new HtmlParser();
var nodes = parser.ParseFragment(source, null);//null = no context given
if (nodes.Length == 0)
Debug.WriteLine("Apparently something bad happened...");
foreach (var node in nodes)
{
// Examine the node
}
Usually all nodes will be IText or IElement types. Also comments (IComment) are possible. You will never see IDocument or IDocumentFragment nodes attached to such an INodeList. However, since HTML5 is quite robust it is very likely that you will never experience "errors" using this method.
What you can do is to look for (parsing) errors. You need to provide an IConfiguration that exposes an event aggregator, which collects such events. The simplest implementation for aggregating only such events (without possibility of adding / removing multiple handlers) is the following:
using AngleSharp.Events;
// ...
class SimpleEventAggregator : IEventAggregator
{
readonly List<HtmlParseErrorEvent> _errors = new List<HtmlParseErrorEvent>();
public void Publish<TEvent>(TEvent data)
{
var error = data as HtmlParseErrorEvent;
if (error != null)
_errors.Add(error);
}
public List<HtmlParseErrorEvent> Errors
{
get { return _errors; }
}
public void Subscribe<TEvent>(ISubscriber<TEvent> listener) { }
public void Unsubscribe<TEvent>(ISubscriber<TEvent> listener) { }
}
The simplest way to use the event aggregator with a configuration is to instantiate a new (provided) Configuration. Here as a sample snippet.
using AngleSharp;
// ...
var errorEvents = new SimpleEventAggregator();
var config = new Configuration(events: errorEvents);
Please note: Every error that is reported is an "official" error (according to W3C spec.). These errors do not indicate that the provided code is malicious or invalid, just that something is not following the spec and that a fallback had to be applied.
Hope this answers your question. If not, then please let me know.
Update Updated the answer for the latest version of AngleSharp.
I was looking at the question Use 'dynamic' throw a RuntimeBinderException. I face a similar problem:
Basically, I want to create a "HTML helper" in ASP.NET MVC that uses dynamic arguments, akin to the htmlArguments parameter for many of the existing helpers (more code below):
public BootstrapCell(Action<string> emitContentAction, dynamic args)
View:
#using (grid.Cell(ViewContext.Writer.Write, new {Position = 4}))
{
<p>zomg!</p>
}
However in the naive approach, i get RuntimeBinderException thrown at me, declaring that 'object' does not contain a definition for 'Position', even though when debugging and hovering over the _args variable, it clearly does have a Position property.
The caller and the callee are in separate assemblies. Why is that problem happening?
(The solution to that has been shown in the same question: Manually create an ExpandoObject to hold the args.)
Implementation:
public class Cell
{
private readonly string _tagName;
private dynamic _args;
private Action<string> EmitContentAction;
public BootstrapCell(Action<string> emitContentAction, dynamic args) : DisposableBaseClass
{
_args = args;
EmitContentAction = emitContentAction;
OnContextEnter();
}
protected void OnContextEnter()
{
var sb = new StringBuilder("<");
sb.Append(_tagName);
if (_args.Position > 0)
{
sb.Append(" class=\"offset");
sb.Append(args.Position);
sb.Append("\"");
}
sb.Append(">");
EmitContentAction(sb.ToString());
}
}
[Edited to make clearer that my problem arises when "obviously" the Position property is set. I am aware that if the property never was defined in the first place, an exception must be raised.]
That code is fatally flawed.
It does work, as long as you specify that property:
void Bar()
{
Foo(new {Position = 0});
}
void Foo(dynamic args)
{
Console.WriteLine(args.Position);
}
That will output 0, it will not throw a RuntimeBinderException.
But the purpose of such code is the possibility for the caller to specify only the properties needed and omit the rest.
You are trying to check for this omission via if(args.Position != null). But that doesn't work, it already requires Position to exist.
When you have a look at the routing API of ASP.NET that also supports those anonymous configuration objects you will notice that the type of the parameter is object and not dynamic.
Using object instead of dynamic will enable your API to be used across assembly boundaries.
So how does it work?
Just like in the linked answer, you need to manually create a dictionary of the properties. Whether you use a plain old Dictionary<string, object> or an ExpandoObject is a matter of preference.
Using ExpandoObject will make your code a bit simpler to read and write, but it is not required.
About the actual exception you are getting:
Please note that it tells you it can't find the Position property on object. If it would be an anonymous type that was missing the Position property the exception message wouldn't refer to object but to an anonymous type. Something like this:
'<>f__AnonymousType0' does not contain a definition for 'Position'
I'm getting started with Astoria/ADO.NET Data Services/WCF Data Services. Looking through a lot of the code samples out there, it appears that the MimeType attribute used to be a method level attribute. After installing the latest update, it is now a class level attribute.
If I have more than one Service Operation that I want to return as a certain MimeType, then it appears now that I have to create a new service for each operation. Is this correct?
Most examples are like this:
[WebGet]
[SingleResult]
[MimeType("application/pdf")]
public IQueryable<byte[]> FooPDF()
{
var result = from p in this.CurrentDataSource.MyPDFs
where p.FooID == 2
select p;
return result.Take(1).Select(p => p.PDF);
}
I get "Attribute 'MimeType' is not valid on this declaration type. It is only valid on 'class' declarations." when I compile, because now I can't do this.
Now, I have to do this:
[MimeType("FooPDF", "application/pdf")]
public class FooService : DataService<FooDBEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetServiceOperationAccessRule("FooPDF", ServiceOperationRights.All);
}
[WebGet]
[SingleResult]
public IQueryable<byte[]> FooPDF()
{
var result = from p in this.CurrentDataSource.MyPDFs
where p.FooID == 2
select p;
return result.Take(1).Select(p => p.PDF);
}
}
What's worse is that I can't add duplicate MimeType attributes to my class.
Is all of this really by design, or am I missing something?
Thanks for reporting this bug to us. I have opened this at our end to track this issue
With the recent update, we added the support for blobs as a first class concept in the data services. If you have a blob association with an entity, then both server and client have ways to surface this. To know more about this, please refer to the following link: http://msdn.microsoft.com/en-us/library/ee473426(v=VS.100).aspx
Hope this helps.
Thanks
Pratik
[MSFT]