clone

Function to clone an arbitrary object

旧时模样 提交于 2019-12-07 09:22:41
问题 I'm looking at a way to clone an object that is not known at compile time (or run-time, I think). The exact wording of the question is "Write a function that can clone an arbitrary object" E.g. Pass unknown object to function. Return a Deep Copy of object. I'm guessing I will need to use Reflection to read the functions and variables, and then some how create a new object and assign these values to it. I could just use the Type.GetType() to find the type and create a new instance, then use

C# Generic Copy Constructor

99封情书 提交于 2019-12-07 05:42:17
问题 I have an interface, and two classes that implements the interface. The classes have generic types. I would like to clone from an instance of one class to the other. interface IFoo { // stuff } class Foo<T> : IFoo { // foo stuff // ifoo implementation } class Bar<T> : IFoo { // bar stuff // ifoo implementation } I have a Foo and would like a Bar. Bar has a copy constructor taking a parameter of IFoo. I created an extension method to implement the clone: public static Bar<T> Clone<T>(this IFoo

what is the need of cloning a object in Java

强颜欢笑 提交于 2019-12-07 05:41:20
问题 I was reading about the cloning in Java, how to make shallow/deep copies of object etc. I was wondering why do I need to create object clones in Java? Any real time examples could be helpful in understanding. 回答1: Quite often you want to use immutable objects, in which case cloning is an essential part of your code. If for example you have an immutable object that has a list or array type field, your getter should always return a clone of the list or array to preserve immutability. The other

How can I get 'git clone --recursive' to clone submodules locally?

孤人 提交于 2019-12-07 05:19:24
问题 I'm in the habit of making a git clone to a master location on my disk and then using a local git clone from there to cut down on download and copy times. However, if I clone a project that has submodules, git clone --recursive , it will use --local for the main repo, but the submodule initialization is still by reference to URLS, which means I'm still going and doing a download again. Is there a way to make git clone --recursive clone from the local copies of submodules instead of the

Permanent casting to a superclass

寵の児 提交于 2019-12-07 05:18:35
问题 If: class Car : Automobile {} I can do: Car toyota = new Car(); Automobile tauto = (Automobile)toyota; but if I do tauto.GetType().Name it will still be Car . Is it possible to perform a cast, so that the type is permanently changed to Automobile (without having to clone the object) ? The problem i am trying to overcome is that there is no multiple inheritance in c#, and i need to merge objects (with the same signature) from 2 services, in one method, and return one type. 回答1: No. There is no

Git, Can't clone repo on windows

隐身守侯 提交于 2019-12-07 05:12:23
问题 I'm trying to use git on windows to clone a remote repository. I can clone it on my mac fine but on windows I get a problem. WHen using git bash to clone, I get a message saying the server's host key is not cached in the registry. It asks me to preess y or n to trust the host. THe problem is that if I press y or n nothing happens. It just hangs there. Should I use OpenSSH instead of PuTTY? Thanks 回答1: The problem is that MSysGit starts PLink in the background, i.e. the terminal is not

gitolite-admin clone issue

a 夏天 提交于 2019-12-07 05:10:56
问题 I am going nuts with a problem cloning the gitolite-admin repository. I have followed this http://sitaramc.github.com/gitolite/install.html#migr and it went perfectly. I ran ssh-keygen -t rsa and scp ~/.ssh/id_rsa.pub morten@ubuntu-server:/tmp/morten.pub authorized_keys on the server looks like this: # gitolite start command="/home/morten/gitolite/src/gitolite-shell morten",no-port-forwarding,no-X11-forwarding,no-agent-forward$ # gitolite end Which AFAIK is okay. When I run git clone morten

Algorithm to clone a graph

为君一笑 提交于 2019-12-07 01:00:37
问题 Algorithm to clone a tree is quite easy, we can do pre-order traversal for that. Is there an efficient algorithm to clone a graph? I tried a similar approach, and concluded we need to maintain a hash-map of nodes already added in the new graph, else there will be duplication of nodes, since one node can have many parents. 回答1: It suffices to do a depth first search and copy each node as it's visited. As you say, you need some way of mapping nodes in the original graph to corresponding copies

How to clone a git repo with all branches and tags from refs/remotes?

北战南征 提交于 2019-12-06 23:47:49
问题 I have a local git repo that I created from an svn repo: $ git svn clone -s svn:... I then created a backup remote and pushed everything to it: $ git remote add backup git@myhost:mybackup.git $ git push --mirror backup Now, when I try to clone from my backup, it is missing all svn tags and branches. $ git clone git@myhost:mybackup.git $ cd mybackup $ git branch -a * master origin remotes/origin/HEAD -> origin/master remotes/origin/master How do I clone the repo with all tags and branches? The

Underscore's Cloning of Mongoose Objects and Deleting Properties Not Working?

半腔热情 提交于 2019-12-06 22:29:08
问题 I'm using Mongoose and I want to remove the _id property from my Mongoose instance before I send the JSON response to the client. Example: var ui = _.clone(userInvite); delete ui["_id"]; console.log(JSON.stringify(ui)); //still has "_id" property, why? The previous didn't work. However, if I do: var ui = JSON.parse(JSON.stringify(userInvite)); //poor man's clone delete ui["_id"]; console.log(JSON.stringify(ui)); //"_id" is gone! it works! I don't understand why calling delete on a cloned