What is the differnce between "using" and an "assembly"? - xamarin.essentials

when I was using Xamarin.Essentials, I needed to add an assembly reference. What is the difference between them? I though both were references. Thanks!

If an assembly A references an assembly B, then A can use all public types that are contained in B. So it's all about making the contents of another assembly known to your assembly.
The using keyword is used within source files to conveniently allow you to refer to types within a certain namespace without having to specify their full name.
You can access any type without using by specifying their full name like
void MyMethod()
{
var myList = new System.Collections.Generic.List<int>();
}
But if you put a using statement into your source, you can refer to the type more easily like
using System.Collections.Generic;
void MyMethod()
{
var myList = new List<int>();
}
So using is really only a convenience feature to make your code more easily written and read.

Related

generating xml containg fields of a component in sdl tridion

How to generate xml file which contain all the fields of a component in SDl Tridion?
I have to do this by writing C# code.
I am using the following code:
public class stockcompany : ITemplate
{
private Engine engine;
private Package package;
public void Transform(Engine engine, Package package)
{
this.engine = engine;
this.package = package;
StringBuilder sb = new StringBuilder();
Component component = engine.GetObject(package.GetValue("Component.ID")) as Component;
ItemFields componentfields = new ItemFields(component.Content, component.Schema);
ItemFields compometa = new ItemFields(component.Metadata, component.Schema);
if (compometa.Contains("check"))
{
}
if (componentfields.Contains("companyname"))
{
string company = string.Empty;
company = componentfields["companyname"].ToString();
package.PushItem("xml", package.CreateHtmlItem(company.ToString()));
XDocument myxml = new XDocument (new XDeclaration ("1.0","UTF-8",null ),new XElement ("companies",new XElement ("company",xml)));
}
You are probably re-inventing the wheel here.
Tridion items are XML. The .Content and .Metadata properties of a component return XML, would this be enough?
DD4T uses templates that publish XML to the delivery side, might be worth checking it: http://code.google.com/p/dynamic-delivery-4-tridion/
Given that DD4T is open sourced, you may want to check how they do it as an example.
Another approach to generating XML is to use an XmlSerializer. It works as follows:
Create a POCO
Fill it with values from the component
Use XmlSerializer to deserialize it into an XML string
Store the XML in the Output variable of the package
This is how DD4T does it. As Nuno said, it is worth while to check it out (see his answer).
I've typically resorted to XSLT to get source XML via a component template, but we can definitely do the same with C#.
[TcmTemplateTitle("Show XML Guts")]
public class ShowXmlGuts : ITemplate
{
public void Transform(Engine engine, Package package)
{
Item contentItem = package.GetByType(ContentType.Component);
Component component = engine.GetObject(contentItem.GetAsSource().GetValue("ID")) as Component;
package.PushItem("componentSource", package.CreateHtmlItem(component.Content.OuterXml));
}
}
Pick-and-Choose Fields
If at all possible, I'd start with an intermediate XML format that's not a one-to-one mapping to component source.
We're meant to get and transform fields with the appropriate APIs. Relying on the source component format could be problematic in your next major change, which could include:
schema changes and new keywords
presentation/rendering side or CMS changes
"unexpected" content (rich text, special characters, tcm references, etc)
Alternatives to C#
In terms of technology:
XSLT is great for "XML generation," even if done in a C# template building block.
The XSLT Mediator would a good choice, if allowed in your environment
You could create XML with DWT, simplifying field selection; however, it's easier to create invalid XML this way (XSLT doesn't validate your XML either, but it's slightly harder to break node nesting)
If possible, update your question with the output XML you're trying to achieve or start a new question to get from your raw component XML to desired output.
Your code isn't very clear and contains a lot of problems, but generally you seem to be on the correct track.
1) You need to cast your
componentfields["companyname"].ToString();
I suspect you're working with a TextField here so cast it to a TextField object and then use the objects .value property
2) Here is where you push the value into the package, this will contain whatever you got from your 'companyname' field, it could be xml, it may not be:
package.PushItem("xml", package.CreateHtmlItem(company.ToString()));
..But I think with this information you can find your way to what you need.
Good luck!

using common methods among multiple classes

I have a couple of class files in C#. I want to write a method that could be used in all the classes. For example, I am trying to write the method that returns the number of rows from the database table, and I need this in multiple times, so thought of writing a single method to share among all the classes. I thought it would be easy with the use of namespace. But when I add namespace in all the class files, it gives error stating "CONTROL NAME is not present in current context". From the internet search I came to the conclusion that I also need to add the namespace in xxx.designer.cs files. Is it correct? I tried to find the designer.cs files but could not, and in one of the solution it was stated that designer.cs file is created during compile time. If so how to add the namespace on designer.cs file.
Thank you!!!
You need to create a static class and this function that classes need to share has to be a static member.
This function can now be called from anywhere.
static class Helper
{
public static string Calculate(int myVariable)
{
//do some common calculation
}
//...
}
If these classes have common data members and you need to share a common function, you can consider using a base class. All common functionality and common data members would go into the base class, and by merit of inheriting that class, all your sub classes would be able to call this function.
Create a Static class and create static member functions into that. You need not to create instance of the class in this case and you can directly call member function using class name.

How do I register types in assemblies that haven't been loaded with Unity?

I want to load all the types of an Interface so I can call a method on it. However, the assemblies are not referenced a compile time. They will be in the bin folder.
Is this something I can do easily with Unity?
So for example I have code sort of like:
using (var container = new UnityContainer())
{
container.RegisterType<IModule>();
var modules = container.ResolveAll(typeof(IModule));
foreach (IModule module in modules) { module.Logon(); }
Console.WriteLine("Done...");
Console.ReadLine();
}
Of course, modules resolves to nothing because the assemblies have been just dropped into the bin folder. They are not statically referenced in my current assembly.
Or, do I have to do some type of Assemblies.LoadAssembly(). I'd like this to be as dynamic as possible. I don't have to have to specify assembly names in a config file or code if possible.
Thanks in advance.
Unity does not, by itself, load any assemblies. It works off Type objects and lets the CLR load those types however it wants to.
To do dynamic discovery like you want, you'll need to write a little code to spin through the assemblies in the bin directory, load them into memory, and then spin through them looking for the types you're interested in. It's pretty trivial if you're familiar with the reflection APIs.
Here's some code you can use to loop through the bin directory and make sure every assembly there is loaded:
private static bool ForceLoadAssemblies()
{
foreach (var fileName in Directory.GetFiles(AppDomain.CurrentDomain.RelativeSearchPath, "*.dll"))
{
string assemblyName = Path.GetFileNameWithoutExtension(fileName);
if (assemblyName != null)
{
Assembly.Load(assemblyName);
}
}
return true;
}
Another option would be to look at MEF instead. MEF was explicitly designed for the dynamic discovery case, while Unity is more built around internal dependency management.

ASP.NET: Custom dynamically populated site map (SiteMapProvider)

I'm trying to write my first very own SiteMapProvider subclass. It is meant to be populated dynamically using a bunch of different database lookups, much like all of the examples I've found on the web.
However, there are a whole bunch of things that are quite unclear to me. Here are my two first questions:
Why is StaticSiteMapProvider used in virtually everyone's project instead of SiteMapProvider? Since the class contains the name "static", I'm getting the impression that it's not as...well, dynamic as I want it.
Can someone provide me with a super-minimalistic SiteMapProvider subclass which populates the map using only static data, i.e. no database access, etc.?
SiteMapProvider can be tottaly dynamic. For example it can make dynamic lookup just for nodes. In contrast with StaticSiteMapProvider you should know whole structure. So this for you to decide what to choose.
You can look at the XmlSiteMapProvider, this is good example of "static" map provider.
public class CoolMapProvider : StaticSiteMapProvider
{
public override SiteMapNode BuildSiteMap()
{
var root = new SiteMapNode(this, "test", "~/test.aspx");
base.AddNode(root, null);
base.AddNode(new SiteMapNode(this, "test-child", "~/test_child.aspx"), root);
return root;
}
}
I did not checked this, but should work.

Solution explorer of visual studio

In solution explorer of asp.net application we are adding something in References section
for eg:in our project there are sample.Dal,sample.exeption ,system.core etc
What is actually References means,,,can we add by 'using' statement
Using is used for namespace resolution. For example:
using System.Data;
lets you access the DataSet class without typing in the fully qualified name; System.Data.DataSet.
This doesn't however tell the compiler what assembly (DLL) the DataSet class lies in. So you need to tell it. So you refer to System.Data.dll by adding it to the references section in solution explorer.
A reference references assemblies required for the current project.
Where using statements reference namespaces for the current file.
And yes, a referenced namespace must exist in one of the referenced assemblies.
Yes, once you reference another project or assembly, it's namespaces and types are available for use in the project that references them (you must reference the project or assembly before you can use the types within it).
You can either use using declarations or fully-qualified type declarations to access the types, as in:
// Example1: The using keyword.
using System;
void Example1()
{
Int32 myExample;
// etc.
}
// Example2: Fully-qualified type.
void Example2()
{
System.Int32 myExample;
// etc.
}
Note: I have used C# here.

Resources