I am wondering why there is no .ToShortDateString in the .NET Portable Class Library. I have 2 projects (Silverlight, and regular .NET Class Library) that use the same code, and the code involves calling .ToShortDateString() on a DateTime object. In order to reuse the same code instead of copying it in 2 places, I created a portable class library so it can be imported by both Silverlight and .NET Class Library. Unfortunately, it doesn't seem like .ToShortDateString() is available when using the class library. I can accept a string parameter in the portable class library method and pass the .ToShortDateString() value from both silverlight and class library projects, but I am wondering why this method isn't native for the portable library. Is it a culture issue?
It was removed to deemphasize its use from what we consider the "modern" surface area, which I hint about here (What is .NET Portable Subset (Legacy)?). This means that it does not show up newer platforms (such as Windows Store apps) and does not show up in portable libraries.
You can mimic its behavior by simply passing "d" to DateTime.ToString().
We wanted to deemphasize its use because it is the only .NET Framework date format that does not have a representation at the Windows OS level. This causes it to not reflect/respect the formatting changes made by the user. In certain organizations and governments, it is important that these settings are respected.
While most of methods/properties that belong to types defined in System namespace are available in PCLs, there are some exceptions, and ToShortDateString is one of them. Below is the list of portable DateTime members. I don't know what was the reason behind the exclusion of some string conversion methods, but I guess this is due to redundancy. As cadrell0 pointed out, you can always achieve the same by using ToString with a parameter.
T:System.DateTime
M:System.DateTime.ToString(System.String)
M:System.DateTime.op_GreaterThan(System.DateTime,System.DateTime)
M:System.DateTime.ParseExact(System.String,System.String[],System.IFormatProvider,System.Globalization.DateTimeStyles)
M:System.DateTime.get_Month
M:System.DateTime.FromFileTimeUtc(System.Int64)
M:System.DateTime.get_Date
M:System.DateTime.get_TimeOfDay
M:System.DateTime.get_Kind
M:System.DateTime.ToUniversalTime
M:System.DateTime.get_Year
M:System.DateTime.op_Subtraction(System.DateTime,System.TimeSpan)
M:System.DateTime.get_Second
M:System.DateTime.get_DayOfWeek
M:System.DateTime.TryParse(System.String,System.IFormatProvider,System.Globalization.DateTimeStyles,System.DateTime#)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32)
M:System.DateTime.get_Day
P:System.DateTime.Date
M:System.DateTime.op_Addition(System.DateTime,System.TimeSpan)
M:System.DateTime.IsDaylightSavingTime
M:System.DateTime.get_DayOfYear
M:System.DateTime.ToFileTime
M:System.DateTime.Subtract(System.DateTime)
M:System.DateTime.IsLeapYear(System.Int32)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.DateTimeKind)
M:System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider,System.Globalization.DateTimeStyles)
P:System.DateTime.Day
M:System.DateTime.get_Hour
M:System.DateTime.Equals(System.DateTime)
M:System.DateTime.get_UtcNow
M:System.DateTime.get_Today
M:System.DateTime.TryParse(System.String,System.DateTime#)
P:System.DateTime.Kind
M:System.DateTime.System#IComparable#CompareTo(System.Object)
P:System.DateTime.UtcNow
P:System.DateTime.Hour
P:System.DateTime.Millisecond
M:System.DateTime.Parse(System.String)
F:System.DateTime.MinValue
M:System.DateTime.op_GreaterThanOrEqual(System.DateTime,System.DateTime)
M:System.DateTime.#ctor(System.Int64,System.DateTimeKind)
M:System.DateTime.GetHashCode
P:System.DateTime.Year
M:System.DateTime.Add(System.TimeSpan)
M:System.DateTime.Equals(System.DateTime,System.DateTime)
M:System.DateTime.ToString(System.IFormatProvider)
M:System.DateTime.get_Now
P:System.DateTime.Month
M:System.DateTime.DaysInMonth(System.Int32,System.Int32)
M:System.DateTime.AddMinutes(System.Double)
M:System.DateTime.get_Minute
M:System.DateTime.#ctor(System.Int64)
M:System.DateTime.op_LessThanOrEqual(System.DateTime,System.DateTime)
M:System.DateTime.ToString(System.String,System.IFormatProvider)
P:System.DateTime.DayOfYear
M:System.DateTime.AddMilliseconds(System.Double)
P:System.DateTime.Second
P:System.DateTime.DayOfWeek
M:System.DateTime.op_Equality(System.DateTime,System.DateTime)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32)
M:System.DateTime.TryParseExact(System.String,System.String,System.IFormatProvider,System.Globalization.DateTimeStyles,System.DateTime#)
M:System.DateTime.ToFileTimeUtc
P:System.DateTime.Today
M:System.DateTime.op_LessThan(System.DateTime,System.DateTime)
M:System.DateTime.get_Millisecond
M:System.DateTime.op_Subtraction(System.DateTime,System.DateTime)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32)
M:System.DateTime.ParseExact(System.String,System.String,System.IFormatProvider)
M:System.DateTime.AddSeconds(System.Double)
M:System.DateTime.AddMonths(System.Int32)
M:System.DateTime.AddYears(System.Int32)
M:System.DateTime.Parse(System.String,System.IFormatProvider,System.Globalization.DateTimeStyles)
M:System.DateTime.get_Ticks
P:System.DateTime.Ticks
M:System.DateTime.TryParseExact(System.String,System.String[],System.IFormatProvider,System.Globalization.DateTimeStyles,System.DateTime#)
M:System.DateTime.ToLocalTime
M:System.DateTime.op_Inequality(System.DateTime,System.DateTime)
M:System.DateTime.SpecifyKind(System.DateTime,System.DateTimeKind)
M:System.DateTime.AddHours(System.Double)
P:System.DateTime.Minute
M:System.DateTime.Subtract(System.TimeSpan)
M:System.DateTime.#ctor(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.DateTimeKind)
F:System.DateTime.MaxValue
M:System.DateTime.ToString
M:System.DateTime.FromFileTime(System.Int64)
P:System.DateTime.TimeOfDay
M:System.DateTime.Compare(System.DateTime,System.DateTime)
M:System.DateTime.CompareTo(System.DateTime)
M:System.DateTime.Parse(System.String,System.IFormatProvider)
M:System.DateTime.AddDays(System.Double)
P:System.DateTime.Now
M:System.DateTime.Equals(System.Object)
M:System.DateTime.AddTicks(System.Int64)
Related
In my projects I have to interact with an ERP system that exposes about 30 different API DLLs.
The ERP company systematically creates new versions of their system and I'm forced to change the DLLs to the new versions. However, not all of my clients update the ERP, so I'm also forced to maintain old references. My current solution to this problem is:
1) For every ERP version I create an 'ApiWrapper' project, referencing the DLLs for this ERP version.
2) In my main project I reference those 'ApiWrappers' and use a selected one of those.
An example function inside of the 'ApiWrapper' looks like this:
public void AddContractorAttribute(Ustawienia settings, string attributeCode, string attributeValue, int contractorId)
{
CreateNewSession();
CDNHeal.Kontrahenci AllContractors = (CDNHeal.Kontrahenci)session.CreateObject("CDN.Kontrahenci", null);
CDNHeal.IKontrahent SelectedContractor = (CDNHeal.IKontrahent)AllContractors[$"Knt_KntId = {contractorId}"];
CDNBase.ICollection AllAttributes = (CDNBase.ICollection)(session.CreateObject("CDN.DefAtrybuty", null));
CDNTwrb1.IDefAtrybut SelectedAttribute = (CDNTwrb1.IDefAtrybut)AllAttributes[$"dea_Kod = '{attributeCode}'"];
CDNTwrb1.IKntAtrybut NewAttributeValue = (CDNTwrb1.IKntAtrybut)SelectedContractor.Atrybuty.AddNew(null);
NewAttributeValue.DefAtrybut = (CDNTwrb1.DefAtrybut)SelectedAttribute;
NewAttributeValue.Wartosc = attributeValue;
session.Save();
}
Now, the problem I have is: if I want to add or change a function in the ApiWrapper, I have to do it in every single one of those. They don't differ code-wise, they only reference a different set of DLLs. Is there a way to solve this issue through class inheritance or otherwise?
You can design a class that will be implemented with the builder pattern or factory method (use some creational pattern, please read about these patterns it will help you reduce your codebase). The approach is to design one generic class which will be used everywhere. So, you can modify it easily. You need to figure out how to distinguish your dlls. Dll's References could be distinguished in two ways:
by the full name of the assembly (usually it includes the version
of assembly).
or you can load all dlls into memory and
reflectively read dll's version.
After distinguishing versions you can easily put all dlls together and ship it.
So, basically you should design a generic builder, which will take a version number as a parameter and return a well-prepared bench of dlls.
Hope it helps.
I'm a bit confused about C#'s use of attributes. At first I thought it was simply used to give program code additional information through the use of the [Obsolete] attribute. Now I find that [Dllimport] can be used to import a dynamic linked library and its functions. Can attributes import .exe files and other kind of files?
A last question, for programmers working in C# every day, how much do you use attributes, and do you use it for anything else than extending information and importing dll's?
Simply said, attributes are just metadata attached to classes or methods, at the very base.
The compiler, however, reads through your code, and runs specific actions for specific attributes it encounters while doing so, hardcoded into it. E.g., when it finds a DllImportAttribute on a method, it will resolve it to an external symbol (again, this is a very simplified explanation).
When it finds an ObsoleteAttribute, it emits a warning of deprecation.
Your own attributes (which you can create with a class inheriting from the Attribute base class) will not have an effect on the default compiler. But you (or other libraries) can also scan for them at runtime, opening up many possibilities and leading to your second question:
I typically use them to do meta programming. For example, imagine a custom network server handling packets of a specific format, implemented in different classes. Each packet format is recognized by reading an integer value. Now I need to find the correct class to instantiate for that integer.
I could do that with a switch..case or dictionary mapping integer -> packet which I extend every time I add a packet, but that is ugly since I have to touch code possibly far away from the actual Packet class whenever I add or delete a packet. I may not even know about the switch or dictionary in case the server is implemented in another assembly than my packets (modularity / extensibility)!
Instead, I create a custom PacketAttribute, storing an integer property set via the attribute, and decorate all my Packet classes with it. The server only has to scan through my assembly types at startup (via reflection) and build a dictionary of integer -> packet pairs automatically. Of course I could scan my assembly every time I need a packet, but that's probably a bit slow performance-wise.
There are APIs which are much more attribute heavy, like controllers in ASP.NET Core: You map full request URLs to methods in handler classes with them, which then execute the server code. Even URL parameters are mapped to parameters in that way.
Debuggers can also make use of attributes. For example, decorating a class with the DebuggerDisplayAttribute lets you provide a custom string displayed for the instances of the class when inspecting them in Visual Studio, which has a specific format and can directly show the values of important members.
You can see, attributes can be very powerful if utilized nicely. The comments give some more references! :)
To answer the second part of your questions, they are also used, for example, in setting validation and display attributes for both client and server side use in a web application. For example:
[Display(Name = "Person's age")]
[Required(ErrorMessage = "Persons's age is required")]
[RangeCheck(13, 59, ErrorMessage = "The age must be between 13 and 59")]
public int? PersonsAgeAtBooking { get; set; }
Or to decorate enums for use in display
public enum YesNoOnlyEnum
{
[Description("Yes")]
Yes = 1,
[Description("No")]
No = 2
}
There are many other uses.
I'm trying to access a few things from FipsDRBG class.
My code is shown below. How do I access fromEntropySource()? This function is within the internal class Base in FipsDRBG.
MyEntropyProvider entropyProvider = new MyEntropyProvider(#params);
FipsDrbg.BuilderService shaAlgo = FipsDrbg.Sha1; // Here I tried to use FipsDrbg.Base but then again it is internal
FipsDrbg.Builder builder = shaAlgo.fromEntropySource(entropyProvider);
I've compared C# FipsDrbg with Java FipsDrbg. In Java, all classes are declared public which is why I can access them easily. I can neither access FipsDrbg.Base or fromEntropySource().
Can someone please suggest how to proceed with this?
I could access all the FIPS related code after getting a paid version of the library. It has great support for all the FIPS related functions which may or may not be available in the free version.
I'm working on a semantic highlighting plugin for VS. Here you can see a web Example.
The goal:
Acquiring all variables and creating different Classifications for every one of them.
The problem:
Getting the variables from the code without writing a C# lexer.
My current approach uses an ITagger. I use an ITagAggregator to get the tags of all the spans that get passed to the ITagger. Then I filter those and get only spans with the "identifier" classification which includes varibles, methods names, class names, usings and properties.
public class Classifier : ITagger<ClassificationTag> {
public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans) {
ITextSnapshot snapshot = spans[0].Snapshot;
var tags = _aggregator.GetTags(spans).Where((span) => span.Tag.ClassificationType.Classification.Equals("identifier")).ToArray();
foreach(var classifiedSpan in tags) {
foreach(SnapshotSpan span in classifiedSpan.Span.GetSpans(snapshot)) {
//generate classification based on variable name
yield return new TagSpan<ClassificationTag>(span, new ClassificationTag(_classification));
}
}
}
}
It would be a lot easier to use the builtin C# Lexer to get a list of all variables bundled to a bunch of meta data. Is this data available for plugin development? Is there an alternative way I could acquire it, if not?
The problem: Getting the variables from the code without writing a C# lexer.
Roslyn can do this: https://roslyn.codeplex.com/
There's even a Syntax Visualizer sample that might interest you. I also found an example using Roslyn to create a Syntax Highlighter.
Visual Studio exposes that information as a code model.
Here is an example how you can access class, and then find attribute on the class, and parse attribute arguments:
Accessing attribute info from DTE
Here is more information about code models:
http://msdn.microsoft.com/en-us/library/ms228763.aspx
Here's also automation object model chart what I've been using quite few times: http://msdn.microsoft.com/en-us/library/za2b25t3.aspx
Also, as said, Roslyn is indeed also a possible option. Here is an example for VS2015 using roslyn: https://github.com/tomasr/roslyn-colorizer/blob/master/RoslynColorizer/RoslynColorizer.cs
For building language tools if may be better to use a parser generator for C#. The GOLD parsing system is one such toolkit which can handle LALR grammars. It has a .NET component based engine that you can use in your project and it can be used to integrate with any IDE. You can also find the grammars for various programming languages including C#.
For a C# UserControl on Windows Mobile (though please answer if you know it for full Windows...it might work) how do you change what shows up in the Designer Properties window for one of the Control's public Properties. For example:
private Color blah = Color.Black;
public Color Blah
{
get { return this.blah; }
set { this.blah = value; }
}
This shows up for the control, but it's in the "Misc" category and has no description or default value. I've tried using the settings in System.ComponentModel like "DesignerCategory", such as:
[DesignerCategory("Custom")]
But says this is only valid for class declarations... could've sworn it was the System.ComponentModel items I used before...
Update:
#John said:
DesignerCatogy is used to say if the
class is a form, component etc.
Try this:
[Category("Custom")]
Is there a particular namespace I need to use in order to get those?
I've tried those exactly and the compiler doesn't recognize them.
In .NETCF all I seem to have available from System.ComponentModel is:
DataObject,
DataObjectMethod,
DefaultValue,
DesignerCategory,
DesignTimeVisible,
EditorBrowsable
The only one it doesn't scream at is EditorBrowsable
DesignerCategory is used to say if the class is a form, component etc.
For full windows the attribute you want is:
[System.ComponentModel.Category("Custom")]
and for the description you can use [System.ComponentModel.Description("This is the description")]
To use both together:
[System.ComponentModel.Category("Custom"),System.ComponentModel.Description("This is the description")]
However this is part of system.dll which may be different for windows mobile.
Is this of use to you? I am not into CF development, but it looks like you need to add some XML metadata to enable it:
http://blogs.msdn.com/bluecollar/archive/2007/02/08/adding-compact-framework-design-time-attributes-or-more-fun-with-textboxes.aspx
Interesting read.. Looks like a lot of design time support was stripped out of CF because you dont design them on the devices.. Which seems kinda weird to me.. Cant imagine using a handheld as a development rig!
Scroll down about half way for the good stuff ;)
The article does not suggest that anyone is designing ON the device. However, when you create a Compact Framework project, the compact framework (for your desktop PC) is used to handle design time rendering. If you think about it that is what you expect. The same framework (or nearly so) is used to do the rendering both on your PC at design time and later on the device at runtime. The issue is that the design time attributes were not added to the compact framework (I assume to reduce the size).